Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/2334442eaa9b8d0b7ff0ab68052bcbf7 to your computer and use it in GitHub Desktop.
Save normanlmfung/2334442eaa9b8d0b7ff0ab68052bcbf7 to your computer and use it in GitHub Desktop.
python_syntax_defaultdict
from collections import defaultdict
word_lists = defaultdict(list)
pairs = [('a', 1), ('b', 2), ('c', 3), ('a', 4), ('b', 5)]
for key, value in pairs:
word_lists[key].append(value) # Append value to list
print(word_lists.get('d', []))
print(word_lists.get('a', []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment