I missed something during the code review. There is an extra unnecessary next call in the first for-loop. This is easily solved by adding another return True.
def all_equal(iterable, key=None):
iterator = groupby(iterable, key)
for first in iterator:
for second in iterator:
return False
return True # <== This was missed
return True
For example, all_equal('a') calls the groupby.__next__ three times.
This is can be made visible by replacing groupby with verbose_groupby:
class verbose_groupby(itertools.groupby):
def __next__(self):
print('N')
return super().__next__()
I missed something during the code review. There is an extra unnecessary
nextcall in the first for-loop. This is easily solved by adding anotherreturn True.For example,
all_equal('a')calls thegroupby.__next__three times.This is can be made visible by replacing
groupbywithverbose_groupby: