Skip to content

Instantly share code, notes, and snippets.

@wiccy46
Created February 10, 2020 14:42
Show Gist options
  • Save wiccy46/3f68d4b5809aaade045faef171306fd1 to your computer and use it in GitHub Desktop.
Save wiccy46/3f68d4b5809aaade045faef171306fd1 to your computer and use it in GitHub Desktop.
[counter] Counting items with defaultdict #python
from collections import defaultdict
item_list = 'spam spam egg egg spam'.split()
count = defaultdict(int)
for item in item_list:
count[item] += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment