Python collections – Part 1 – Defaultdict

Defaultdict is available as part of collections module in Python that overcome some drawbacks of using native dict. Let us see why and when to use a defaultdict.

Consider a scenario when you are trying to access a key which is not present in dict. It will straightaway throw a keyerror. Again, it can be explicitly handled but then there needs to be an additional check. This can be avoided by using a defaultdict.

>>> d = {"a": 1}
>>> d["a"]
1
>>> d["b"]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: 'b'

Above piece of code will throw a key error when a key that is not present in the dict is accessed. If defaultdict is used for the same, it will behave like this:

>>> from collections import defaultdict
>>> dd = defaultdict(dict)
>>> dd.update({"a": 1})
>>> dd["a"]
1
>>> dd["b"]
{}

In this case KeyError was not encountered. Other operations can also be done in similar fashion without worrying about KeyError.

Another example where defaultdict is a savior is when updating a nested dict in which parent is not present.

>>> d = {"fruit": {}}
>>> d["fruit"]["mango"] = 10
>>> d
{'fruit': {'mango': 10}}
>>> d["vegetable"]["potato"] = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'vegetable'

When replaced with defaultdict,

>>> from collections import defaultdict
>>> dd = defaultdict(dict)
>>> dd.update({"fruit": {}})
>>> dd
defaultdict(<type 'dict'>, {'fruit': {}})
>>> dd["fruit"]["mango"] = 10
>>> dd["vegetable"]["potato"] = 5
>>> dd
defaultdict(<type 'dict'>, {'vegetable': {'potato': 5}, 'fruit': {'mango': 10}})

In this case no error was found. In fact, a new dict was created vegetable automatically with child having potato key.

So, whenever there is a possibility of getting a keyerror  in any piece of code, it is better to use defaultdict instead of dict.

Source

 

The post Python collections – Part 1 – Defaultdict appeared first on PyJournal.