Python - Dictionnaires imbriqués
Dictionnaires imbriqués
Un dictionnaire peut contenir des dictionnaires, c'est ce qu'on appelle des dictionnaires imbriqués.
Exemple
Créez un dictionnaire contenant trois dictionnaires :
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
Ou, si vous souhaitez ajouter trois dictionnaires dans un nouveau dictionnaire :
Exemple
Créez trois dictionnaires, puis créez un dictionnaire qui contiendra les trois autres dictionnaires :
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}