List Iteration#
The \(for\) loop allows you to iterate over each of the elements in a list.
liste = [1, 2, 3, 4, 5]
for element in liste:
print(element)
1
2
3
4
5
This method allows access to the elements of a list, but we may also need
their indices, and for this we use the function \(range()\).
Todo
Introduire cela plus loin, juste avant le(s) exercice(s) pour les manipuller.
Utiliser une liste non numérique pour l’exemple. Donner un exemple pour lequel cela ait un intérêt d’avoir simultanément l’indice et la valeur.
Reminder: The range() function
The range
function returns a sequence of integers. It is often used in
loops to repeat an action a certain number of times. It can be used in
three forms:
range(fin)
: generates a sequence of integers from0
tofin
(exclusive).range(début, fin)
: generates a sequence of integers fromdébut
tofin
(exclusive).range(début, fin, pas)
: generates a sequence of integers fromdébut
tofin
(exclusive), in steps ofpas
.
liste = [10, 6, 3, 9, 0]
for i in range(len(liste)): #pour i allant de 0 à 5 exclus (la taille de la liste)
print("l'élément", liste[i], "est à l'indice", i) #On affiche l'élément liste[i] et l'indice i
l'élément 10 est à l'indice 0
l'élément 6 est à l'indice 1
l'élément 3 est à l'indice 2
l'élément 9 est à l'indice 3
l'élément 0 est à l'indice 4
In the following example where we want to replace certain elements of the list, we
need the indices:
notes = [12, 5, 14, 7, 18, 6, 19]
# Parcours avec les indices pour modifier la liste en place
for i in range(len(notes)):
if notes[i] < 10:
notes[i] = 10 # Remplacer les notes en dessous de 10 par 10
print(notes) # Résultat attendu : [12, 10, 14, 10, 18, 10, 19]
[12, 10, 14, 10, 18, 10, 19]
You may also want to compare consecutive elements:
temperatures = [20, 22, 21, 23, 25, 24, 26]
# Comparer des éléments consécutifs pour trouver les baisses de température
for i in range(1, len(temperatures)):
if temperatures[i] < temperatures[i - 1]:
print ("La température a baissé de", temperatures[i - 1], "à", temperatures[i], "au jour", i)
La température a baissé de 22 à 21 au jour 2
La température a baissé de 25 à 24 au jour 5
Here is another example of traversing a list: a function that takes a list as
input and returns the sum of all its elements.
def somme(liste):
somme = 0
for i in liste:
somme += i
return somme
print(somme([1, 2, 3, 4, 5]))
print(somme([0, -1, -2, -3, -4]))
print(somme([]))
15
-10
0
Exercise
Define the functions maximum
and minimum
that return the maximum
and the minimum of a list of integers, respectively.
def maximum(liste):
### BEGIN SOLUTION
m = liste[0]
for element in liste:
if element >= m:
m = element
return m
### END SOLUTION
# Tests pour la fonction maximum
assert maximum([1, 2, 3, 4, 5]) == 5
assert maximum([-10, -5, -2, -3]) == -2
assert maximum([10]) == 10
def minimum(liste):
### BEGIN SOLUTION
m = liste[0]
for element in liste:
if element <= m:
m = element
return m
### END SOLUTION
# Tests pour la fonction minimum
assert minimum([1, 2, 3, 4, 5]) == 1
assert minimum([-10, -5, -2, -3]) == -10
assert minimum([10]) == 10
Exercise
Define the function dernierIndiceMaximum
that returns the index of the maximum of a
list.
If the maximum appears multiple times in the list, we want the index of its last occurrence.
def dernierIndiceMaximum(liste):
### BEGIN SOLUTION
m = liste[0]
longueur=len(liste)
indice_max = 0
for i in range(longueur):
if liste[i] >= m:
m = liste[i]
indice_max = i
return indice_max
### END SOLUTION
assert dernierIndiceMaximum([1, 2, 3, 4, 5]) == 4
assert dernierIndiceMaximum([1, 5, 3, 5, 4]) == 3
assert dernierIndiceMaximum([7, 7, 7, 7]) == 3
assert dernierIndiceMaximum([10]) == 0