I've read the examples in python docs, but still can't figure out what this method means. Can somebody help? Here are two examples from the python docs
>>> from collections import defaultdict
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
... d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
and
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
the parameters int
and list
are for what?
Usually, a Python dictionary throws a KeyError
if you try to get an item with a key that is not currently in the dictionary. The defaultdict
in contrast will simply create any items that you try to access (provided of course they do not exist yet). To create such a "default" item, it calls the function object that you pass to the constructor (more precisely, it's an arbitrary "callable" object, which includes function and type objects). For the first example, default items are created using int()
, which will return the integer object 0
. For the second example, default items are created using list()
, which returns a new empty list object.
Is it functionally different than using d.get(key, default_val) ?
@Ambareesh
d.get(key, default)
won't ever modify your dictionary – it will just return the default and leave the dictionary unchanged.defaultdict
, on the other hand, will insert a key into the dictionary if it isn't there yet. This is a big difference; see the examples in the question to understand why.How do we know what is the default value for each type? 0 for int() and [] for list() are intuitive, but there can also be more complex or self-defined types.
@Sean
defaultdict
calls whatever constructor you pass in. If you pass in an a typeT
, values will be constructed usingT()
. Not all types can be constructed without passing in any parameters. If you want to construct such a type, you need a wrapper function, or something likefunctools.partial(T, arg1, arg2)
.Or even more easily: a lambda.
defaultdict(lambda : T(arg1, arg2))
.