Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Incrementally Building a Dictionary

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.

Avatar image for Minh Pham

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

Avatar image for John Kinder

John Kinder on Aug. 31, 2020

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

Avatar image for rsa000

rsa000 on Dec. 7, 2021

Heyo,

I noticed the video for this segment seems to be cropped a little too much and I can’t fully see all the elements in the dictionaries being created for examples.

Not really a problem, just thought I’d point it out.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 8, 2021

@rsa000 Hey, thanks for pointing that out! You’re right. However, although the code seems cropped, you can actually work it out. Here it is for the convenience of others:

my_profile = {}
my_profile["fname"] = "Paul"
my_profile["lname"] = "Mealus"
my_profile["city"] = "San Diego"
my_profile["past_jobs"] = ["analyst", "sandwich maker", "system engineer"]
my_profile["transportation"] = {"skateboard": "quest", "bike": "trek"}

Become a Member to join the conversation.