Let's deep dive into Python dictionary and Python defaultdict()
class
Python Dictionaries
Dict is one of the data structures available in Python which allows data to be stored in the form of key-value pairs.
Example:
d = {'a': 2, 'b': 5, 'c': 6}
Problem with Dictionary
Dictionaries work well unless you encounter missing keys. Suppose you are looking for a key-value pair where there is no value in the dictionary - then you might encounter a KeyError
problem. Something like this:
d = {'a': 2, 'b': 5, 'c': 6}
d['z'] # z is not present in dict so it will throw a error
You will see something like this:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
d['z']
KeyError: 'z'
Solution to the above problem
To overcome the above problem we can use different ways:
Using inbuilt functions
setdefault
If the key
is in the dictionary, return its value. If not, insert a key with a value of default
and return default
. default
defaults to None
:
>>> d = {'a' :2, 'b': 5, 'c': 6}
>>> d.setdefault('z', 0)
0 # returns 0
>>> print(d) # add z to the dictionary
{'a': 2, 'b': 5, 'c': 6, 'z': 0}
get
Return the value for key
if the key is in the dictionary, else default
. If the default is not given, it defaults to None
, so that this method never raises a KeyError
:
>>> d = {'a': 2, 'b': 5, 'c': 6}
>>> d.get('z', 0)
0 # returns 0
>>> print(d) # Doesn't add z to the dictionary unlike setdefault
{'a': 2, 'b': 5, 'c': 6}
The above 2 methods are the solutions to our problem. It never raises KeyError
. Apart from the above 2 methods, Python also has a collections
module that can handle this problem. Let's dig deep into the defaultdict
in the collections module:
defaultdict
defaultdict
can be found in the collections module of Python. You can use it using:
from collections import defaultdict
d = defaultdict(int)
defaultdict
constructor takes default_factory
as an argument that is a callable. This can be for example:
int
: default will be an integer value of 0
str
: default will be an empty string ""
list
: default will be an empty list []
Code:
from collections import defaultdict
d = defaultdict(list)
d['a'] # access a missing key and returns an empty list
d['b'] = 1 # add a key-value pair to dict
print(d)
output will be defaultdict(<class 'list'>, {'b': 1, 'a': []})
The defaultdict
works the same as the get()
and setdefault()
methods, so when to use them?
When to use get()
If you specifically need to return a certain key-value pair without KeyError
and also it should not update in the dictionary - then dict.get
is the right choice for you. It returns the default value specified by you but does not modify the dictionary.
When to use setdefault()
If you need to modify the original dictionary with a default key-value pair - then setdefault
is the right choice.
When to use defaultdict
setdefault
method can be achieved using defaultdict
but instead of providing default value every time in setdefault
, we can do it at once in defaultdict
. Also, setdefault
has a choice of providing different default values for the keys. Both have their own advantages depending on the use case.
When it comes to efficiency:
defaultdict
> setdefault()
or get()
defaultdict
is 2 times faster than get()
!
You can check the results here.