Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Nesting Dictionaries

00:00 Values of dictionaries can have any valid Python type. That means a dictionary item can have another dictionary as its value. Just as you can nest lists inside other lists and tuples inside other tuples, you can create nested dictionaries. On the right side, you can see the capitals dictionary, but I altered the dictionary to illustrate the idea of a nested dictionary.

00:26 The dictionary is now named states. The keys are still "California", "New York", and "Texas", but the values are now dictionaries instead of strings.

00:37 The nested dictionaries contain the key-value pairs for "capital" and "flower". Let’s iterate over the dictionary and print the keys and values.

00:48 for state, facts in states.items(): Indent because you are in the body of a for loop. print(state, facts).

01:08 When you run the code, you see the keys, California, New York, and Texas, and right next to it, the dictionary which is the value that corresponds to this key.

01:21 But let’s say that we only want to print the flower of the state instead of the whole dictionary of facts. This you already learned in the last lesson, and it’s exactly the same.

01:33 You use facts and then ["flower"] to access the "flower" key in the facts dictionary.

01:46 When you run the code, then you see printed on a left side, California California Poppy New York Rose and Texas Bluebonnet.

01:55 But what if you want to access the nested value directly? If you want to access a nested value, you can chain the square brackets. To access the capital of Texas directly, you can type

02:11 print(state["Texas"]it’s important here that you use the capital T for "Texas" because dictionary keys are case-sensitive—and after that, another square bracket with "capital", which is a key of the nested dictionary.

02:35 Then you have to close the print() function.

02:40 And when you run the code, you should see the capital directly. But we get a TypeError, and that’s because we used state instead of states.

02:50 We tried to access an index of the variable state, which exists in our code, but it’s not what we wanted. We wanted to access the keys "Texas" and "capital" of the states dictionary.

03:05 So now when we run it and run the code again, then you see that in the end, on the left side, we print Austin, which is the capital city of Texas.

03:18 Nesting dictionaries might look a bit complicated even if you don’t run into errors, but nested dictionaries come up more often than you might expect. They’re particularly useful when working with data transmitted over the Web.

03:33 Nested dictionaries are also great for modeling structured data such as spreadsheets or relational databases. And like with any data structure or basically anything in Python, the more you use it, the more you get used to it.

03:49 Another way of improving your Python skills is to test your knowledge. In the next lesson, you can challenge yourself with an exercise on Python dictionaries.

Become a Member to join the conversation.