Skip to content

Simplify Generator

Sourcery refactoring id: simplify-generator

Description:

An identity generator (a for a in coll) can be replaced directly with the collection coll

Before:

if any(hat for hat in wardrobe.hats):
    print("I have a hat!")

After:

if any(wardrobe.hats):
    print("I have a hat!")

Explanation:

The expression (x for x in y) is a generator that returns all of the elements of y. If being passed into a function like any or all that takes a generator or sequence, it can simply be replaced by y which is much clearer.