4 minute read

Introduction

Python has many ways of creating and using dictionaries (or maps), so it is no surprise that many beginners often feel overwhelmed by Python dictionaries. In this tutorial, we will go over the main pros and cons of using default dictionaries, as well as many examples on how to use them.

What is a default dictionary in Python?

Default dictionaries are very similar to regular Python dictionaries (or dict), both are used to store key-value pairs of many data types.

However, the main benefit of using defaultdict is that they create a default value for new added key to the dictionary. Therefore, it is no longer necessary to check if a certain key exists in the dictionary before modifying the value, given that defaultdicts will create a default value for that key.

For instance, if we were to add the key pizza and incremetn its value by 1, we would need to first initiate to 0 if we were using a normal dictionary. This is not necessary for defaultdicts as we will learn next.

How do we use default dictionaries in Python?

Creating and using default dict is very straightforward and easy to understand. First of all, you may create a new defaultdict by specifying what the default value type will be just like:

my_first_default_dict = defaultdict(int)`

Here, we create our first default dict by specifying int as our default value. By specifying int, new keys will have an initial value of 0.

We may specify other values like strings, lists, among many more.

Example

Let us assume that we have a food_dict defaultdict which will store how many pieces of food we have.


food_dict = defaultdict(int) # Creates a new default dict of type int (with their default value as 0)
food_dict["pizza"] += 15 # Increments the value by 15

print(food_dict["pizza"]) # Prints 15

As you can see above, we declared food_dict which is a defaultdict of type int. As explained, this means that their default value will be 0.

Next, we increment food_dict["pizza"] by 1, thus getting 15 in the output. Note that we did not need to manually initiate the value to be 0 as we would using normal dictionaries.

For instance, if we had a normal dictionary we would need to type this instead:


food_dict = dict() # Creates a new dictionary
food_dict["pizza"] = 0
food_dict["pizza"] += 15 # Increments the value by 15

print(food_dict["pizza"]) # Prints 15

Just like that, we need to manually specify that the original value is 0 which makes it slightly harder to read and takes more time. Note that if we need to declare the original value since we would get a KeyError otherwise, as the key does not yet exist.

Should I use default dicts or normal dictionaries in Python?

Many beginners will doubt whether they should use default dict or just normal Python dictionaries. Our answer is: it depends on your usecase.

For instance, defaultdicts save time by initiating the defaultvalue but this means that all values will initially be of the specified type. If we wanted to store multiple data types, there would not be a real benefit of using defaultdicts.

On the other hand, it is often unclear what a normal Python dictionary stores which can make code harder to read.

Therefore, if the value type, which will be stored in the dictionary, will always be the same (like ints, strings, lists, etc.) our answer would be to use default dicts whenever possible.

However, if the desired value type is a tuple or it may change in the future, it is recommended to use regular Python dictionaries.

Conclusion

Defaultdicts are a great way to store data efficiently when the desired default value type is knownn and not prone to change in the future.

They ensure that the value type is known and improve readability.