Skip to content

Enhancement: Add a [consider-using-generator]-checker in any or all calls #3165

@tweakimp

Description

@tweakimp

Problem:
There are many cases of comprehensions inside of any or all calls that are unnecessary and should be replaced by a generator.

For example:

some_list = list(range(1000))
test_old = any([x % 7 == 0 for x in some_list])

Instead, a generator would be less code, and way faster:

test_new = any(x % 7 == 0 for x in some_list)

Speed comparisons:

64 µs ± 1.67 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) # old
447 ns ± 3.48 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) # new

any and all are functions that profit from that because the loop is breaking and can short cut; the remaining elements are not even looped over if the test returns a True in the any function or a False in the all function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentGood first issueFriendly and approachable by new contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions