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.
Become a Member to join the conversation.