bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python - Nested Dictionaries

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python - Nested Dictionaries?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ = {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Loop Through Nested Dictionaries
Access Items in Nested Dictionaries
Nested Dictionaries

Nested Dictionaries

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

Example

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

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(myfamily["child2"]["name"])

Loop Through Nested Dictionaries

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

Example

for x, obj in myfamily.items():

  print(x)

  for y in obj:

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

Previous

Python - Copy Dictionaries

Next

Python Dictionary Methods