Skip to content

Simplify Generator

Sourcery refactoring id: simplify-generator


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


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


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


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.