Created
March 30, 2024 22:33
-
-
Save normanlmfung/2334442eaa9b8d0b7ff0ab68052bcbf7 to your computer and use it in GitHub Desktop.
python_syntax_defaultdict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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