3 Structures
Python features several different basic integrated structures. In this section we will discuss some of them: lists, tuplets, sets and dictionaries.
3.1 Lists
One of the most flexible structures in Python is the list. It is a grouping of values. The creation of a list is done by writing the values by separating them with a comma and surrounding them by square brackets ([
and ]
).
## ['Pascaline', 'Gauthier', 'Xuan', 'Jimmy']
The content of a list is not necessarily text:
## [1, 2, 3, 4, 5]
It is even possible to include elements of different types in a list:
## ['Piketty', 'Thomas', 1971]
A list can contain another list:
tweets = ["aaa", "bbb"]
followers = ["Anne", "Bob", "Irma", "John"]
compte = [tweets, followers]
print(compte)
## [['aaa', 'bbb'], ['Anne', 'Bob', 'Irma', 'John']]
3.1.1 Extraction of the Elements
Access to the elements is made thanks to its indexation (be careful, the index of the first element is 0):
## Pascaline
## Gauthier
Access to an element can also be done by starting from the end, by putting the minus sign (-
) in front of the index:
## Jimmy
## Xuan
Splitting a list so as to obtain a subset of the list is done with the colon (:
):
## ['Gauthier']
## ['Xuan', 'Jimmy']
## ['Pascaline', 'Gauthier']
When extracting items from the list using the brackets, it is possible to add a third argument, the step :
## ['Pascaline', 'Xuan']
Access to nested lists is done by using the brackets several times:
tweets = ["aaa", "bbb"]
followers = ["Anne", "Bob", "Irma", "John"]
conuts = [tweets, followers]
res = conuts[1][3] # The 4th item of the 2nd item on the list counts
The number of elements in a list is obtained with the function len()
:
## 2
## 4
3.1.2 Modification
Lists are mutable, i.e., their content can be modified once the object has been created.
3.1.2.1 Replacement
To modify an element in a list, the indexes can be used:
## [1, 3, 5, 7, 9]
3.1.2.2 Adding Elements
To add items to a list, the method append()
can be used:
## [1, 3, 5, 7, 9, 11]
It is also possible to use the extend()
method, to concatenate lists:
## [1, 3, 5, 7, 9, 11, 13, 15]
3.1.2.3 Deleting Elements
To removing an item from a list, the method remove()
can be used:
## [1, 5, 7, 9, 11, 13, 15]
The del
command can also be used:
## [1, 3, 5, 9]
3.1.2.4 Multiple assignments
Several values can be modified at the same time:
## [1, 3, 5, 7, 9]
The modification can increase the size of the list:
## [1, 2, 'a', 'b', 'c', 'd', 4, 5]
Several values can be deleted at the same time:
## [1, 2, 3]
3.1.3 Verifying if a Value is Present
By using the operator in
, it is possible to test the belonging of an object to a list:
## True
3.1.4 Copy of List
Be careful, copying a list is not trivial in Python. Let’s take an example.
Let’s modify the first element of y
, and look at the content of y
and x
:
## [0, 2, 3]
## [0, 2, 3]
As can be seen, using the equal sign simply created a reference and not a copy.
To copy a list, there are several ways to do so. Among them, the use of the list()
function:
## x : [1, 2, 3]
## y : [0, 2, 3]
It can be noted that when a splitting is done, a new object is created, not a reference:
## x : [1, 2, 3, 4]
## y : [0, 2]
3.1.5 Sorting
To sort the objects in the list (without creating a new one), Python offers the method sort()
:
## [1, 2, 3, 4]
It also works with text values, sorting in alphabetical order:
## ['a', 'a', 'b', 'c']
It is possible to provide the sort()
method with arguments. Among these arguments, there is one, key
, which provides a function for sorting. This function must return a value for each object in the list, on which the sorting will be performed. For example, with the len()
function, which, when applied to text, returns the number of characters:
## ['a', 'aa', 'aa', 'aaaaa']
3.2 Tuples
The tuples are sequences of Python objects.
To create a tuple, one lists the values, separated by commas:
## (1, 4, 9, 16, 25)
It should be noted that tuplets are identified by a series of values, surrounded in two brackets.
3.2.1 Extraction of the Elements
The elements of a tuple are extracted in the same way as those in the lists (see Section 3.1.1).
## 1
3.2.2 Modification
Unlike lists, tuplets are inalterable (i.e. cannot be modified after they have been created):
## Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: 'tuple' object does not support item assignment
##
## Detailed traceback:
## File "<string>", line 1, in <module>
It is possible to nest tuplets inside another tuple. To do this, parentheses are used:
## ((1, 4, 9, 16), (1, 8, 26, 64))
3.3 Sets
Sets are unordered collections of unique elements. The sets are unalterable, not indexed.
To create a set, Python provides the set()
function. One or more elements constituting the set are provided, separated by commas and surrounded by braces ({}
):
## {'Marseille', 'Aix-en-Provence', 'Rennes', 'Nice'}
Equivalently, rather than using the set()
function, the set can only be defined using the brackets:
## {'Marseille', 'Aix-en-Provence', 'Rennes', 'Nice'}
On the other hand, if the set is empty, Python returns an error if the set()
function is not used:
il est nécessaire d’utiliser la fonction set :
## <class 'dict'>
The type of the object we have just created is not set
but dict
(i.e. Section 3.4). Also, to create the empty set, we use set()
:
## <class 'set'>
During the creation of a set, if there are duplicates in the values provided, these will be deleted to keep only one value:
## {'Marseille', 'Aix-en-Provence', 'Rennes', 'Nice'}
The length of a set is obtained using the len()
function:
## 4
3.3.1 Modifications
3.3.1.1 Adding Elements
To add an element to a set, Python offers the add()
method:
## {'Aix-en-Provence', 'Marseille', 'Toulon', 'Rennes', 'Nice'}
If the element is already present, it will not be added:
## {'Aix-en-Provence', 'Marseille', 'Toulon', 'Rennes', 'Nice'}
3.3.1.2 Deletion
To remove a value from a set, Python offers the method remove()
:
## {'Aix-en-Provence', 'Marseille', 'Rennes', 'Nice'}
If the value is not present in the set, Python returns an error message:
## Error in py_call_impl(callable, dots$args, dots$keywords): KeyError: 'Toulon'
##
## Detailed traceback:
## File "<string>", line 1, in <module>
## {'Aix-en-Provence', 'Marseille', 'Rennes', 'Nice'}
3.3.2 Belonging test
One of the advantages of sets is the quick search for presence or absence of values (faster than in a list). As with the lists, the belonging tests are performed using the operator in
:
## True
## False
3.3.3 Copying a Set
To copy a set, as for lists (c.f. Section ??), the equality sign should not be used. Copying a set is done using the copy()
method:
new_set = set({"Marseille", "Aix-en-Provence", "Nice"})
y = new_set.copy()
y.add("Toulon")
print("y : ", y)
## y : {'Marseille', 'Toulon', 'Aix-en-Provence', 'Nice'}
## set : {'Marseille', 'Aix-en-Provence', 'Nice'}
3.3.4 Conversion to a List
One of the interests of sets is that they contain unique elements. Also, when you want to obtain the distinct elements of a list, it is possible to convert it into a set (with the set()
function), then to convert the set into a list (with the list()
function):
## ['Marseille', 'Aix-en-Provence', 'Marseille', 'Marseille']
## {'Marseille', 'Aix-en-Provence'}
## ['Marseille', 'Aix-en-Provence']
3.4 Dictionaries
Python dictionaries are an implementation of key-value objects, the keys being indexed.
Keys are often text, values can be of different types and structures.
To create a dictionary, you can proceed by using braces ({}
). As encountered in the Section 3.3, if we evaluate the following code, we get a dictionary :
## <class 'dict'>
To create a dictionary with entries, the braces can be used. Each entry is separated by commas, and the key is distinguished from the associated value by two points (:
):
my_dict = { "nom": "Kyrie",
"prenom": "John",
"naissance": 1992,
"equipes": ["Cleveland", "Boston"]}
print(my_dict)
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Cleveland', 'Boston']}
It is also possible to create a dictionary using the dict()
function, by providing a sequence of key-values:
## {'Julien-Yacine': 'Data-scientist', 'Sonia': 'Director'}
3.4.1 Extraction of the Elements
Extraction from dictionaries is based on the same principle as for lists and tuples (see Section @ref(#structure-liste-extraction)). However, the extraction of an element from a dictionary is not based on its position in the dictionary, but by its key:
## John
## ['Cleveland', 'Boston']
If the extraction is done by a key not present in the dictionary, an error will be returned:
## Error in py_call_impl(callable, dots$args, dots$keywords): KeyError: 'age'
##
## Detailed traceback:
## File "<string>", line 1, in <module>
You can test the presence of a key with the operator in
:
## True
## False
The extraction of values can also be done using the get()
method, which returns a None
value if the key is not present:
## John
## None
3.4.2 Keys and values
Using the key()
method, the keys of the dictionary can be accessed:
## dict_keys(['nom', 'prenom', 'naissance', 'equipes'])
## <class 'dict_keys'>
It is then possible to transform this key enumeration into a list:
## ['nom', 'prenom', 'naissance', 'equipes']
The values()
method provides the dictionary values:
## dict_values(['Kyrie', 'John', 1992, ['Cleveland', 'Boston']])
## <class 'dict_values'>
The items()
method provides keys and values in the form of tuples:
## dict_items([('nom', 'Kyrie'), ('prenom', 'John'), ('naissance', 1992), ('equipes', ['Cleveland', 'Boston'])])
## <class 'dict_items'>
3.4.3 Search for Belonging
Thanks to the methods keys()
, values()
and items()
, it is easy to search for the presence of objects in a dictionary.
## False
## True
## True
3.4.4 Modification
3.4.4.1 Replacement
To replace the value associated with a key, the brackets ([]
) and the equality sign (=
) can be used.
For example, to replace the values associated with the team
key:
my_dict["equipes"] = ["Montclair Kimberley Academy",
"Cleveland Cavaliers", "Boston Celtics"]
print(my_dict)
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics']}
3.4.4.2 Adding Elements
Adding an element to a dictionary can be done with brackets ([]
) and the equality sign (=
):
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics'], 'taille_cm': 191}
To add the content of another dictionary to a dictionary, Python offers the update()
method.
Let’s create a second dictionary first:
## {'masse_kg': 88, 'debut_nba': 2011}
Let’s add the content of this second dictionary to the first:
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics'], 'taille_cm': 191, 'masse_kg': 88, 'debut_nba': 2011}
If the second dictionary is subsequently modified, it will not affect the first:
## {'masse_kg': 88, 'debut_nba': 2011, 'poste': 'PG'}
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics'], 'taille_cm': 191, 'masse_kg': 88, 'debut_nba': 2011}
3.4.4.3 Deleting elements
There are several ways to delete an element in a dictionary. For example, with the operator del
:
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics'], 'taille_cm': 191, 'masse_kg': 88}
It is also possible to use the pop()
method:
## {'nom': 'Kyrie', 'prenom': 'John', 'naissance': 1992, 'equipes': ['Montclair Kimberley Academy', 'Cleveland Cavaliers', 'Boston Celtics'], 'taille_cm': 191}
In the previous instruction, we added an assignment of the result of applying the pop()
method to a variable named res
. As can be seen, the pop()
method, in addition to deleting the key, returned the associated value:
## 88
3.4.5 Copy of a Dictionary
To copy a dictionary, and not create a reference (which is the case if you use the equality sign), Python provides, as for sets, a copy()
method:
## d: {'Marseille': 13, 'Rennes': 35}
## d2: {'Marseille': 13, 'Rennes': 35, 'Paris': 75}
3.4.6 Exercise
Create a dictionary named
photo
, including the following key-value pairs:- key:
id
, value:1
, - key:
description
, value:A photo of the Old Port of Marseille
, - key:
loc
, value: a list in which the following coordinates are given5.3772133
,43.302424
.
- key:
add the following key-value pair to the
photo
dictionary: key :user
, value :bob
.Look for an entry with a key that is worth
description
in thephoto
dictionary. If this is the case, display the corresponding entry (key and value).Delete the entry in
photo
whose key isuser
.Modify the value of the entry
loc
in thephoto
dictionary, to propose a new list, whose coordinates are as follows:5.3692712
and43.2949627
.