I am trying to understand the usefulness of the itertools.groupby()
function and have created a rather naive use case. I have a list of numbers, and want to group them by oddness or evenness. Below is my code using itertools.groupby()
:
for decision, group in groupby(range(2, 11), key=lambda x: x % 2 == 0):
...: print(decision, list(group))
And below is the output I get:
True [2]
False [3]
True [4]
False [5]
True [6]
False [7]
True [8]
False [9]
True [10]
Basically what I was expecting is something like where all the "True's" are grouped together and all the "False's" are grouped together.
Is this even possible with groupby()
?