Skip to content

Instantly share code, notes, and snippets.

@tomislater
Created October 6, 2013 16:13
Show Gist options
  • Select an option

  • Save tomislater/6855881 to your computer and use it in GitHub Desktop.

Select an option

Save tomislater/6855881 to your computer and use it in GitHub Desktop.
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for x in ["hell", "camp", "four", "walls", "only", "hell", "pity", "party"]:
>>> d[x] += 1
>>> print d["hell"]
2
>>> print d["pity"]
1
>>> print d
defaultdict(<type 'int'>, {'pity': 1, 'camp': 1, 'only': 1, 'four': 1, 'walls': 1, 'party': 1, 'hell': 2})
>>> d = defaultdict(list)
>>> for x in ["hell", "four", "walls", "hell", "pity", "party"]:
>>> d[x].append(True)
>>> print d["hell"]
[True, True]
>>> print d["walls"]
[True]
>>> print d
defaultdict(<type 'list'>, {'four': [True], 'hell': [True, True], 'walls': [True], 'pity': [True], 'party': [True]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment