What is a Python defaultdict and when to use it?
4 min readMar 16, 2023
Python has a datatype called dictionary (or dict) in which you can map values to keys. But you can also import the similar defaultdict datatype from the standard collections library.
A defaultdict is a dictionary that can be given a default value. Which means that when you try to work with a key that is not in the dictionary yet, a default value is used.