Skip to content

Simplify Empty Collection Comparison

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: simplify-empty-collection-comparison

Section titled “Sourcery refactoring id: simplify-empty-collection-comparison”

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

if a == "":
do_anything()
while l == []:
handle(l)
if not a:
do_anything()
while not l:
handle(l)

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.