Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 3, 2020 09:44
Show Gist options
  • Save raeq/becf11802e4ca664aacea8d01ea16780 to your computer and use it in GitHub Desktop.
Save raeq/becf11802e4ca664aacea8d01ea16780 to your computer and use it in GitHub Desktop.
Using a defaultdict
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