Skip to main content

Replace Comparisons to Empty Sequence with Implicit Boolean Comparison


ImportanceReview GuidanceRequires Scanning Tool
LowMerge After ReviewNo

Empty sequences in Python always evaluate to False. This means that comparison expressions that use empty sequences can sometimes be simplified. In these cases no explicit comparison is required: instead we can rely on the truth value of the object under comparison. This is sometimes referred to as "implicit" comparison. Using implicit boolean comparison expressions is considered best practice and can lead to better code.

Our changes look like the following:

 x = [1]

- if x != []:
+ if x:

If you have feedback on this codemod, please let us know!


Why is this codemod marked as Merge After Review?

Values compared to empty sequences should be verified in case they are falsy values that are not a sequence.

Codemod Settings