defaultdict with a parameter to the class constructor
>>> def f():
return 1
>>> a = defaultdict(f)
>>> a[1]
1
Here is an other implementation using lambda expression (from kindall):
>>> a = defaultdict(lambda: 1)
defaultdict(lambda: 1)
eg
>>> from collections import defaultdict
>>> a = defaultdict(lambda: 1)
>>> a["foo"] += 1
>>> a["foo"]
2