A Popular Data Structure In Python Programming : The List

in STEMGeeks4 years ago

Python has a great built-in sequence data structure and there are six built-in types of sequences. Such as below -

  1. String
  2. List
  3. Tuples
  4. Byte sequences
  5. Byte array
  6. Range object

A sequence is a group of items with a fixed ordering which means every item of a sequence is assigned a number according to its position or index. The starting position or index is 0.
The most common operations can be done with all sequence types, like indexing, adding, slicing, multiplying, checking for membership.

In this article, I'll discuss some operations of the Python list.

code-1084923_1280.png

Source

Python list :

A list is an ordered data structure in Python, it is similar to arrays in JavaScript that is a mutable, or changeable and used to store collections of data. We know to create a string, the elements are placed inside single (' ') or double quotation (" ")but for creating the list, the elements must be placed inside square brackets ( [ ] ) and separated by commas. Every element that is inside of a list container is called an item.

Python lists are a perfect solution to group related values into one container and it is so easy to perform operations on multiple values at once.

Suppose a mega shop, where there are many types of products, and each product is grouped according to a specific category and placed in rows so that the product is easy to find.

Create a list:

It is simple to create a list using square brackets [ ] .
For example :

#an empty list
lit = []    

#A list with elements 
 fruits = ['mango' , 'apple', 'orange', 'banana', 'pear']
 print(fruits)

Output:
['mango', 'apple', 'banana', 'pear']

The above list contains string data type as a single type (homogeneous) but we can also create a list with different types of data (heterogeneous). Look below -

 hetero = [3, 'mango' ]
 print(hetero)

Output:
[3, 'mango']

The list can also contain duplicated elements as below :

dup = ['mango','mango']
print(dup)

Output:
['mango', 'mango']

A python list can also be created using a built-in list() constructor with iterable elements . Iterable elements mean sequence data (string), collection (tuple, dictionary, set,) and the range object.

lt = list('Hello World')
print(lt)

Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

#To create empty list using list() constructor
emp = list()
print(emp)
[]

#String data types element
list('promethian')
['p', 'r', 'o', 'm', 'e', 't', 'h', 'i', 'a', 'n']

#Set data types element
list({'name','age','country'})
['age', 'name', 'country']

#Dictionary data types element
list({'Rahim':21,'Karim':25,'Balel':27})
['Rahim', 'Karim', 'Balel']

#Tuple data types element
list(('love','hate','survive'))
['love', 'hate', 'survive']

#Range object
list(range(21))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

List Comprehension

Python list can also be created using "List Comprehension" concept. It may be a tricky method but easy to understand.

For loop applied:
cph = [] for it in range(10): cph.append(it) print(cph)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List Comprehension applied:

cph = [it for it in range(10)]
print(cph)

Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Accessing items in list

As ordered sequences of elements, each element is indexed using a starting index of 0 ( [0] ) in the Python list. Each element can be called individually through indexing, the index (numbering) inside square brackets as bellow -

list1edited.png

#a group of fruits
fruits = ['Mango','Banana','Pineapple','Waterlemon','Orange','Apple']

# access the first element
fruits[0]

Output:
'Mango'

# access the second element
fruits[1]

Output:
'Banana'

# access the last element with a negative value
fruits[-1]

Output:
'Orange'

# to know the total number of elements in this group
len(fruits)

Output:
6

# access the first element with a negative value
fruits[-6]

Output:
'Mango'

Since indexing starts at 0, so the indexing of 6 elements will be 0 to 5.If we call any element from our fruits list with a greater positive index number than 5, it will return an error.

Fruits[6]

Output:
IndexError: list index out of range

*********************************