patternpythonCriticalCanonical
Collections.defaultdict difference with normal dict
Viewed 0 times
withcollectionsdefaultdictdifferencedictnormal
Problem
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
and
the parameters
>>> from collections import defaultdict
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
... d[k] += 1
...
>>> d.items()
dict_items([('m', 1), ('i', 4), ('s', 4), ('p', 2)])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?Solution
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.Context
Stack Overflow Q#5900578, score: 909
Revisions (0)
No revisions yet.