Skip to content

Instantly share code, notes, and snippets.

@monk1337
Created October 13, 2017 12:53
Show Gist options
  • Save monk1337/d79aba7434df8586d7e4a34615f1603b to your computer and use it in GitHub Desktop.
Save monk1337/d79aba7434df8586d7e4a34615f1603b to your computer and use it in GitHub Desktop.
a=[(2010,2),(2009,4),(1989,8),(2009,7)]
new_dict={}
for i in a:
if i[0] not in new_dict:
new_dict[i[0]]=[i[1]]
else:
try:
new_dict[i[0]].append(i[1])
except KeyError:
pass
print(new_dict)
#output
{2009: [4, 7], 2010: [2], 1989: [8]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment