Skip to content

One more nit on all_equal() #902

@rhettinger

Description

@rhettinger

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__()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions