bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python/Foundations Practice
Python•Foundations Practice

Python - Nested Dictionaries

Concept visual

Python - Nested Dictionaries

keys map to buckets01kiwi:12pear:43apple:7grape:9

Nested Dictionaries

A dictionary can contain dictionaries, this is called nested dictionaries.

Example

Create a dictionary that contain three dictionaries:

myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}

Or, if you want to add three dictionaries into a new dictionary:

Example

Create three dictionaries, then create one dictionary that will contain the other three dictionaries:

child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}

Access Items in Nested Dictionaries

To access items from a nested dictionary, you use the name of the dictionaries, starting with the outer dictionary:

Example

Print the name of child 2:

print(myfamily["child2"]["name"])

Loop Through Nested Dictionaries

You can loop through a dictionary by using the items() method like this:

Example

Loop through the keys and values of all nested dictionaries: for x, obj in myfamily.items():

print(x)

for y in obj:

print(y + ':', obj[y])

Previous

Python - Remove Dictionary Items

Next

Python Dictionary Exercises