liste_numbers = [1, -18, 3]
liste_str = ["France", "Spain", "Italy"]5-Lists
Link to the Google Colaboratory notebook: 5-Lists
A sequence is a data structure in Python.
Each element of a sequence is assigned with an index (its position).
The index starts from 0 and increases by one through the sequence.
Python lists
The list is a datatype available in Python.
It is written as a list of comma-separated values (items) between square brackets.
A list needs may have elements of different type.
list_test = ["Evry", 77]
print(type(list_test[0]), type(list_test[1]))<class 'str'> <class 'int'>
It is nonetheless recommended to have elements of the same type for easiest manipulation.
Accessing values
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.
list_test = ["France", "Spain", "Italy"]
print(list_test[0])
print(list_test[-1])
print(list_test[:2])
print(list_test[2:])France
Italy
['France', 'Spain']
['Italy']
Modify a list
You can modify an element of the list using the = sign:
list_test = ["France", "Spain", "Italy"]
list_test[0] = "Portugal"
print(list_test)
list_test[:2] = ["Portugal", "England"]
print(list_test)['Portugal', 'Spain', 'Italy']
['Portugal', 'England', 'Italy']
You can also add an element to the list using the append function:
list_test = ["France", "Spain", "Italy"]
list_test.append("Portugal")
print(list_test)['France', 'Spain', 'Italy', 'Portugal']
You can delete an element of the list using the del function:
list_test = ["France", "Spain", "Italy"]
del list_test[0]
print(list_test)['Spain', 'Italy']
List operations
Here are some list operations, that differ from string or number operations for lists:
| Python expression | Results | Description |
|---|---|---|
len([1, 2, 3]) |
3 | Length |
[1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Concatenation |
['Evry'] * 4 |
['Evry', 'Evry', 'Evry', 'Evry'] |
Repetition |
3 in [1, 2, 3] |
True |
Membership |
for x in [1, 2, 3]: print x |
1 2 3 |
Iteration |
List copy
You can not copy a list by using list2=list1, because it will only create a reference to list1. Changes in list1 will affect list2.
This is a very common mistake in the beginning of Python programming.
list1 = [0, 5, 10]
list2 = list1 # A bad copy: list2 will behave like list1
del list1[0]
print(list1, list2)[5, 10] [5, 10]
Instead, use the .copy() function that will allocate a new space in memory for the new list.
list1 = [0, 5, 10]
list2 = list1.copy() # A good copy: list2 will be independant from list1
del list1[0]
print(list1, list2)[5, 10] [0, 5, 10]
List comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
Let’s say we want the elements from a list that contains the e letter.
We can do using a loop and another list:
list1 = ["evry", "apple", "spain", "france"]
list2 = []
for element in list1:
if "e" in element:
list2.append(element)
print(list2)['evry', 'apple', 'france']
With list comprehension you can do all that with only one line of code:
list1 = ["evry", "apple", "spain", "france"]
list2 = [element for element in list1 if "e" in element]
print(list2)['evry', 'apple', 'france']
The syntax is the following: newlist = [expression for item in iterable if condition == True]
The return value is a new list, leaving the old list unchanged.