In Python, a dictionary is an unordered collection of data values where each element is a key/value pair. Dictionaries have a wide variety of applications including mapping objects in video games, database settings, unit testing and much more. In this post, we will discuss how to define and perform simple operations on dictionaries in python.
Let’s get started!
DICTIONARIES WITH SETS AND LISTS
The simple definition of a dictionary is a data structure that maps a key to a single value. Most of the time dictionaries are used to map keys to multiple values. To do this, we need to map dictionary keys to containers, like sets and lists. For example, suppose we have a dictionary that contains movie names and metacritic ratings:
movie_ratings = {'movie_name': ['Parasite', 'Once Upon a Time in Hollywood', 'Marriage Story'], 'rating': [96, 83, 93]}
In this dictionary, ‘movie_name’ is a key that is mapped to a list containing three movies. Similarly, ‘rating’ is another key that is mapped to three movie ratings. You can also define a similar dictionary using sets:
movie_ratings = {'movie_name': {'Parasite', 'Once Upon a Time in Hollywood', 'Marriage Story'}, 'rating': {96, 83, 93}}
where a set is an unordered collection objects with no duplicates. If you want to maintain the insertion order of items in the collection, you will want to use lists. If you want to have no duplicates in your collection and order does not matter, you will want to use sets.
DEFAULT DICTIONARIES
An easy way define dictionaries with keys mapped to either list or sets is using ‘defaultdict’. Let’s construct our first dictionary, which used lists, using ‘defaultdict’. First, let’s import ‘defaultdict’:
from collections import defaultdict
Let’s initialize our movie ratings dictionary as a list dictionary:
movie_ratings = defaultdict(list)
A nice feature of default dictionaries in python, is that they automatically intialize the first value so you can primarily focus on inserting values. Let’s insert the three movie names:
movie_ratings['movie_name'].append('Parasite')
movie_ratings['movie_name'].append('Once Upon a Time in Hollywood')
movie_ratings['movie_name'].append('Marriage Story')
Now, let’s print our dictionary:
print(movie_ratings)

and next let’s insert the movie ratings:
movie_ratings['rating'].append(96)
movie_ratings['rating'].append(83)
movie_ratings['rating'].append(93)
Let’s print again:
print(movie_ratings)

Now let’s add a duplicate. Let’s insert ‘Parasite’ and its rating:
movie_ratings['movie_name'].append('Parasite')
movie_ratings['rating'].append(96)
And let’s print:
print(movie_ratings)

We see that we’ve successfully inserted a duplicate. Let’s try to do the same for a set dictionary. Let’s initialize our set dictionary:
movie_ratings = defaultdict(set)
We’ll now add our movie names. Note that with sets we use the ‘add()’ method, not to be confused with the ‘append()’ method we used for lists:
movie_ratings['movie_name'].add('Parasite')
movie_ratings['movie_name'].add('Once Upon a Time in Hollywood')
movie_ratings['movie_name'].add('Marriage Story')
print(movie_ratings)

Next let’s add our ratings:
movie_ratings['rating'].add(96)
movie_ratings['rating'].add(83)
movie_ratings['rating'].add(93)
print(movie_ratings)

Now let’s try to add a duplicate. Let’s add ‘Parasite’ and its rating again:
movie_ratings['movie_name'].add('Parasite')
movie_ratings['rating'].add(96)
print(movie_ratings)

We can see that our dictionary hasn’t been modified. This is because sets don’t allow duplicates. I’ll stop here but I encourage you to look into some additional important dictionary methods in python. For example, you can use the ‘.items()’ method to iterate over key/value pairs:
for key, value in movie_ratings.items():
print(key, value)

Other important methods you can look into are ‘.keys()’, ‘.get()’, and ‘.setdefault()’.
CONCLUSIONS
To summarize, in this post we discussed how to define and operate on dictionaries in python. We went over how to define dictionaries with lists and sets. We also discussed how we can use ‘defaultdict’ to initialize these dictionary types which allow us to focus on data insertion. I hope you found this post useful. The code from this post is available on GitHub. Thank you for reading!