Created
August 3, 2020 09:44
-
-
Save raeq/becf11802e4ca664aacea8d01ea16780 to your computer and use it in GitHub Desktop.
Using a 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 as dd | |
s = [("John", "Male"), ("John", "48"), ("John", "Married"), ("Jane", "Female"), | |
("Jane", "25")] | |
dict1: dict = dd(list) | |
for k, v in s: | |
dict1[k].append(v) | |
assert dict1["John"] == ["Male", "48", "Married"] | |
assert dict1["Jane"] == ["Female", "25"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment