nameerror name 'defaultdict' is not defined code example
Example 1: nameerror name 'defaultdict' is not defined
You're not importing defaultdict. Do either:
from collections import defaultdict
or
import collections
d = collections.defaultdict(list)
Example 2: nameerror name 'defaultdict' is not defined
import collections
d = collections.defaultdict(int)
d
defaultdict(<type 'int'>, {})