Dictionaries

Python ↔ Baobab
13 correspondences

A Bao dictionnaire is a Python dict: key → value pairs, bracket access, cles()/valeurs()/elements() methods for keys()/values()/items().

PythonBaobabDescription
d = {"a": 1}d = {"a": 1}Literal creation — identical syntax.
d[cle] / d[cle] = vd[cle] / d[cle] = vRead and write by key.
d.keys()d.cles()View of the dictionary's keys.
d.values()d.valeurs()View of the values.
d.items()d.elements()View of the (key, value) pairs.
d.get(cle, defaut)d.obtenir(cle, defaut)Safe read: return default if the key is missing.
d.pop(cle)d.retirer(cle)Remove a key and return its value.
d.update(autre)d.mettre_a_jour(autre)Merge another dictionary into d.
d.clear()d.vider()Empty the dictionary.
d.copy()d.copier()Shallow copy.
cle in dcle dans dKey existence test.
len(d)longueur(d)Number of pairs.
del d[cle]supprimer d[cle]Delete an entry.
Complete example — Baobab ↔ Python
Baobab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
notes = {"maths": 15, "physique": 12} notes["chimie"] = 17 # ajout notes.mettre_a_jour({"maths": 16}) afficher(notes.obtenir("histoire", 0)) # → 0 (clé absente) afficher("maths" dans notes) # → vrai afficher(longueur(notes)) # → 3 pour matiere, note dans notes.elements(): afficher(f"{matiere}: {note}/20") afficher(liste(notes.cles())) # → ["maths", "physique", "chimie"] afficher(somme(notes.valeurs())) # → 45 retiree = notes.retirer("physique") afficher(retiree) # → 12
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
notes = {"maths": 15, "physique": 12} notes["chimie"] = 17 # add notes.update({"maths": 16}) print(notes.get("histoire", 0)) # → 0 (missing key) print("maths" in notes) # → True print(len(notes)) # → 3 for matiere, note in notes.items(): print(f"{matiere}: {note}/20") print(list(notes.keys())) # → ["maths", "physique", "chimie"] print(sum(notes.values())) # → 45 retiree = notes.pop("physique") print(retiree) # → 12