Introduction
Une liste est une suite de variables alignées dans une boite. Il s’agit d’un type de variable très intéressante et très flexible. Tu les rencontreras extrêmement souvent.

Objectifs
- ✅ Comprendre ce qu’est une liste
- ✅ Manipuler des listes
Pré-requis
Avoir validé la ressource suivante :
Voir la ressource “2.3 Python - Introduction aux listes”
Les listes

Comme dit plus haut, une liste est une structure de données permettant de regrouper des données dans une boîte.
Comme les string, les listes sont des objets, comme à peu près tout en Python (plus de détails dans une prochaine ressource).
💡Contrairement aux strings, les listes sont mutables. Les éléments d’une liste peuvent être modifiés par d’autres éléments, tout en gardant la même id.
list_name = ["Léon", "René", "Bernard"]
print(id(list_name))
# 140194799193536 (le nombre sera différent chez toi)
list_name[0] = "Babar"
print(id(list_name))
# 140194799193536 : le même id, c'est toujours la même liste
Un objet liste permet d’effectuer des actions (méthodes) différentes des objets string (logique, ce n’est pas le même type d’objet).
On peut parcourir une liste, accéder à un élément, modifier un élément à une certaine position, supprimer un élément, effectuer des calculs (par exemple additionner tous les nombres d’une liste)…
En tant que boîte, une liste peut contenir tout ce qu’on veut.
Elle peut même contenir d’autres listes !

En fonction de l’agencement de la liste, elle aura une ou plusieurs dimensions, exactement comme les matrices en mathématiques.
Bref la liste est très pratique.
Les ressources t’aideront à en apprendre davantage. Après les avoir regardées, passe au challenge ;-)
Comment itérer sur une list ?
https://www.geeksforgeeks.org/iterate-over-a-list-in-python/
Les listes python - python doctor
https://python.doctor/page-apprendre-listes-list-tableaux-tableaux-liste-array-python-cours-debutant
Sélectionner les données dans une liste
Python Lists - W3School
https://www.w3schools.com/python/python_lists.asp
Challenge
Dans ce challenge tu vas t’entraîner à la manipulation de listes.
Crée une copie du notebook ci-après et réalise les missions qui s’y trouve : 2.4 Python - Lists
Tu trouveras aussi tous les exercices du notebook ci-dessous. Les consignes sont en anglais.
Preliminary note: sometimes, the challenge tells you to do something that is incompatible with some input (for instance, you ask for a position but the user enters some text, or an invalid number).
Here you can assume that all user input goes according to plan (the “happy path”). If you want an extra optional challenge, feel free to manage possible error cases!
Mission 1
Define a list, then display only the elements (and their position) that have even positions (0, 2, 4, 6…).
With test_list = ["a", "b", "c", "d", "e"], the script should display:
a at position 0
c at position 2
e at position 4
Mission 2
Define a list, then ask for an index, followed by a string. Then, replace the list element at that index with the given string and display the resulting list.
For example, with the list test_list2 = ['hello', 'good morning', 'bye bye', 'have a good day'], the index 2, and the string yeah, you should display:
['hello', 'good morning', 'yeah', 'have a good day']
Mission 3
Define a list with five numbers, then ask the user for a number. Put the number at the end of the list, remove the first element from the list, and finally display the list.
For example, with the list test_list3 = [1, 9, 6, 15, 4] and the input 7, you should display:
[9, 6, 15, 4, 7]
Mission 4
Define a list, then ask the user for a string. Then, insert the string in the list at a random position, and display the resulting list.
For example, with the list test_list4 = ["p", "y", "t", "h", "o"] and the input n, you might get:
["p", "n", "y", "t", "h", "o"]
… but you might also get:
["p", "y", "t", "n", "h", "o"]
Mission 5
Create a list of integers. Then, display a list with the elements ordered from smallest to largest, using the right built-in function.
With the list test_list5 = [3, 4, 0, -1, 35, 7], this will display:
[-1, 0, 3, 4, 7, 35]
Mission 6
Ask the user for a string, convert it to a list of characters with the list function, then ask for a single character.
Remove the first occurrence of the character from the list, then display how many occurrences of the character remain in the list. Uppercase and lowercase count as the same character (E and e).
For instance if you enter "Excellent work" then the letter "e", it will display:
There are still 2 copies of e in the list
Mission 7
Define three lists containing no duplicates. Then, indicate (however you like) the two that have the most elements in common (or all three if there’s a tie, that is if several pairs share the highest number of common elements).
For instance, with:
list_a = [1, 2, 4, 8, 16, 32]
list_b = [1, 2, 3, 5, 8, 13]
list_c = [2, 3, 5, 7, 11, 13]
It may display:
Lists B and C have the most common elements!
Critères de réussite
- Les exercices ont été correctement codés