Lists

Python ↔ Baobab
16 correspondences

A Bao liste is a Python list: mutable, ordered, heterogeneous. Every Python method has its French counterpart — liste.ajouter(x) is exactly list.append(x).

PythonBaobabDescription
l.append(x)l.ajouter(x)Append x at the end of the list.
l.extend(autre)l.etendre(autre)Append all items from another list.
l.insert(i, x)l.inserer(i, x)Insert x at position i.
l.remove(x)l.retirer(x)Remove the first occurrence of x.
l.pop() / l.pop(i)l.depiler() / l.depiler(i)Remove and return the last item (or the one at index i).
l.clear()l.vider()Empty the whole list.
l.index(x)l.indice(x)Index of the first occurrence of x.
l.count(x)l.compter(x)Number of occurrences of x.
l.sort()l.trier()Sort the list in place (ascending).
l.sort(reverse=True)l.trier(inverse=vrai)Sort in descending order.
l.reverse()l.inverser()Reverse the items in place.
l.copy()l.copier()Return a shallow copy of the list.
len(l)longueur(l)Number of items.
l[0], l[-1], l[1:3]l[0], l[-1], l[1:3]Indexing and slicing — identical to Python.
a + b, a * 3a + b, a * 3List concatenation and repetition.
x in lx dans lMembership test.
Complete example — Baobab ↔ Python
Baobab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fruits = ["mangue", "banane"] fruits.ajouter("papaye") # append fruits.inserer(1, "ananas") # insert fruits.etendre(["kiwi", "cola"]) # extend afficher(fruits) # → ["mangue", "ananas", "banane", "papaye", "kiwi", "cola"] fruits.retirer("kiwi") # remove dernier = fruits.depiler() # pop → "cola" afficher(dernier) afficher(fruits.indice("banane")) # → 2 afficher(fruits.compter("mangue")) # → 1 afficher(longueur(fruits)) # → 4 fruits.trier() # sort fruits.inverser() # reverse afficher(fruits[0]) # premier élément afficher(fruits[-1]) # dernier élément afficher(fruits[1:3]) # tranche afficher("mangue" dans fruits) # → vrai
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fruits = ["mangue", "banane"] fruits.append("papaye") fruits.insert(1, "ananas") fruits.extend(["kiwi", "cola"]) print(fruits) # → ["mangue", "ananas", "banane", "papaye", "kiwi", "cola"] fruits.remove("kiwi") dernier = fruits.pop() # → "cola" print(dernier) print(fruits.index("banane")) # → 2 print(fruits.count("mangue")) # → 1 print(len(fruits)) # → 4 fruits.sort() fruits.reverse() print(fruits[0]) # first item print(fruits[-1]) # last item print(fruits[1:3]) # slice print("mangue" in fruits) # → True