From the course: Programming Foundations: Data Structures
Create a dictionary in Python - Python Tutorial
From the course: Programming Foundations: Data Structures
Create a dictionary in Python
- [Instructor] Let's create a dictionary in Python. We'll use a dictionary to store states in their capitals where the state is the key and the capital is the value. To create an empty dictionary, we use curly braces. We can initialize it with a few states and capitals on creation. Let's give it the state, Texas, and its capital Austin. The syntax is fairly simple. The key comes first, followed by colon, and then the value. Let's add another entry. We'll add New York with its capital Albany. Each key value entry in a dictionary is separated by a comma. To access a value in our dictionary, we use a similar methodology as we do to access an item in a list. However, instead of a numerical index, we use the key as the index. Let's print out the capital of New York to the console. We'll access the dictionary and then index with the key New York. Let's run it. And there's the capital of New York, Albany. Now, a dictionary is actually built off of lists behind the scenes, but all that logic is abstracted away from us. We don't need to worry about how the dictionary is implemented internally, what matters is understanding how to work with the dictionary at an abstract level, knowing the expected inputs, outputs, and the effect it has on our data.
Contents
-
-
-
-
-
What is a dictionary?1m 23s
-
Create a dictionary in Python1m 40s
-
(Locked)
Mutate a dictionary in Python3m 5s
-
(Locked)
Solution: Update salary53s
-
(Locked)
Handle missing dictionary keys2m 2s
-
(Locked)
Retrieve keys and value collections from dictionaries1m 44s
-
(Locked)
Solution: Clean user preferences1m 27s
-
(Locked)
When to use dictionaries1m 22s
-
-
-
-
-
-
-