Search code examples
pythondictionarydefaultdict

Replace collections' defaultdict by a normal dict with setdefault


I often used collections.defaultdict to be able to append an element to d[key] without having to initialize it first to [] (benefit: you don't need to do: if key not in d: d[key] = []):

import collections, random
d = collections.defaultdict(list)  
for i in range(100):
    j = random.randint(0,20)
    d[j].append(i)  # if d[j] does not exist yet, initialize it to [], so we can use append directly

Now I realize we can simply use a normal dict and setdefault:

import random
d = {}
for i in range(100):
    j = random.randint(0,20)
    d.setdefault(j, []).append(i)

Question: when using a dict whose values are lists, is there a good reason to use a collections.defaultdict instead of the second method (using a simple dict and setdefault), or are they purely equivalent?


Solution

  • collections.defaultdict is generally more performant, it is optimised exactly for this task and C-implemented. However, you should use dict.setdefault if you want accessing an absent key in your resulting dictionary to result in a KeyError rather than inserting an empty list. This is the most important practical difference.