Created
October 27, 2020 11:45
-
-
Save Kush1101/1ed70b315bb03adfc42ffd5ebff7e811 to your computer and use it in GitHub Desktop.
This file contains 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 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