Skip to content

Commit

Permalink
fix defaultdict in collections.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
trishnaguha committed Aug 10, 2015
1 parent ca933b1 commit 9c3976e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ defaultdict example
-------------------

::

>>> from collections import defaultdict
>>> 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])]
dict_items([('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])])

In the example you can see even if the key is not there in the defaultdict object, it automatically creates an empty list. *list.append* then helps to append the value to the list.

Expand Down

0 comments on commit 9c3976e

Please sign in to comment.