Skip to content

Simplify Empty Collection Comparison

Sourcery refactoring id: simplify-empty-collection-comparison

Description:

Replace an empty collection comparison with a more idiomatic unary operator.

Before:

if a == "":
    do_anything()

while l == []:
    handle(l)

After:

if not a:
    do_anything()

while not l:
    handle(l)

Explanation:

This refactoring follows the PEP 8 Programming Recommendations, specifically the recommendation that

For sequences, (strings, lists, tuples), use the fact that empty sequences are false

in the context of comparisons.

Back to top