Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Kush1101
Created October 27, 2020 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kush1101/1ed70b315bb03adfc42ffd5ebff7e811 to your computer and use it in GitHub Desktop.
Save Kush1101/1ed70b315bb03adfc42ffd5ebff7e811 to your computer and use it in GitHub Desktop.
from itertools import groupby
a_list = [("Even", 2),
("Odd", 5),
("Even", 8),
("Odd", 3)]
iterator = groupby(a_list, key = lambda x:x[0])
for key, group in iterator:
key_and_group = {key : list(group)}
print(key_and_group)
# OUTPUT
"""
{'Even': [('Even', 2)]}
{'Odd': [('Odd', 5)]}
{'Even': [('Even', 8)]}
{'Odd': [('Odd', 3)]}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment