Want to share your content on python-bloggers? click here.
The supporting repository and code file is: https://github.com/StatsGary/PyHacks-Tutorials/blob/main/28_defaultdict.py.
This is aimed as a short article to explain why you would want to use default dictionaries. What is a defaultdict I hear you ask?
A defaultdict is a container like dictionary present in the collections module. The functionality of both dictionaries and defaultdict is most the same, except for the fact that if a normal dictionary does not have a key then it would raise a key error, whereas a defaultdict wouldn’t do this, instead if would return a default value.
Let’s use this in practice.
Example 1
Here we will create a simple defaultdict to reinforce the point highlighted earlier:
To break this down:
- We define a custom value to return as part of a function
- We initialise our defaultdict as dd
- We start adding keys to the dictionary
- We then print out the relevant keys and the following is returned:
We can observe that our default return string has been printed.
Let’s further reinforce this example and discuss why defaultdicts are useful.
Under the hood of the defaultdict function
Under the hood the defaultdict functions adds one writable class instance variable and one method in addition to your standard dictionary function. The instance variable is the default_factory parameter and the method provided is __missing__. The default_factory is a function for returning the default value for the dictionary defined. If this argument is absent, then the dictionary would raise a KeyError.
Let’s try this:
The default message passed to the lambda (anonymous function) shows the following:
Using the defaultdict as an integer (int)
When the int class is passed to the default_factory argument of the defaultdict function, then a defaultdict is created with a default value as zero. See below:
The output of this is a default dictionary with a list of indexes and associated values.
These approaches are good when you don’t want a key value error to mess up your code. It allows each new key to be given a default value based on the type of dictionary being created.
A defaultdict
can be created by giving its declaration an argument that can have three values; list, set or int. According to the specified data type, the dictionary is created and when any key, that does not exist in the defaultdict
is added or accessed, it is assigned a default value as opposed to giving a KeyError
.
That is the only difference between normal dictionaries and default dictionaries and are mostly used when adding new entities to a dictionary and for other algorithms where a keyerror would cause an iteration statement to break
Want to share your content on python-bloggers? click here.