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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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.

Incrementally Building a Dictionary

Give Feedback

Defining a dictionary using curly braces and a list of key-value pairs is fine if you know all the keys and values in advance. But what if you want to build a dictionary on the fly?

You can start by creating an empty dictionary, which is specified by empty curly braces. Then you can add new keys and values one at a time. Once the dictionary is created in this way, its values are accessed the same way as any other dictionary. Retrieving the values in the sublist or subdictionary requires an additional index or key.

You can use dictionaries for a wide range of purposes because there are so few limitations on the keys and values that are allowed. But there are some, as you’ll see in the next lesson.

00:00 If you want to build a dictionary on the fly, you can initialize an empty dictionary with empty curly braces. Then you can add to the dictionary as needed.

00:11 You could potentially do this a lot when your program is dynamic and needs temporary data structures that grow and shrink in sizes. Let’s hop back into the console and play with growing a dictionary incrementally.

00:24 Think back to the earlier example of a social media profile and let’s play with that scenario a little bit. If we start an empty dictionary using empty curly braces, we can add to it on the fly. And think that perhaps you have a program that is doing this programmatically.

00:51 Building up this profile with your first name, your last name, maybe you’ve got your city in there.

01:07 And check this out. You can make a list

01:16 a value in your dictionary. So if we say—ah, I’m missing an o here—our 'past_jobs' is going to be a list of our past three jobs, something like that. You can give it a list for a value.

01:38 I think I spelled sandwich wrong. Whatever.

01:49 Now we have a list in our dictionary. And same with the dictionary—you can put a dictionary within a dictionary, and that’s called nesting. I can’t spell today.

02:04 If we give it the key 'transportation', we can give that a dictionary.

02:20 And now that we’ve built this profile out a bit, we can work with it like any dictionary. Maybe just list the whole thing, or select certain keys. And to access our list within the my_profile dictionary, we can call the list by the key and then further, we can index that list.

02:46 So if I called just the list, which was 'past_jobs', you see it returns our list. And then if I want to index it and just get 'sandwhich maker', I call index 1. Likewise, to access the 'transportation' dictionary, we need to use the key and then we can work with the keys within that nested 'transportation' dictionary.

03:12 And that looks like this, so if we call ['transportation'], we see our dictionary and we see we’ve got the keys for 'skateboard' and 'bike', so let’s call ['bike']. And that works like that!

03:26 Now that you’ve got a little bit of firsthand experience with dictionaries, this is probably a good time to talk about restrictions on keys and values.

Minh Pham on March 22, 2020

Hello Paul,

Could you please provide text of what your wrote in the video so that we can try it out ourselves

BR Minh

John Kinder on Aug. 31, 2020

Anyone who has a Trek bike is OK in my book. ;)

Become a Member to join the conversation.