C1801 len-as-condition

Message

'Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty'

Description

Used when Pylint detects that len(sequence) is being used without explicit comparison inside a condition to determine if a sequence is empty. Instead of coercing the length to a boolean, either rely on the fact that empty sequences are false or compare the length against a scalar.

Example

def example():
    items = [1]
    if len(items):
        pass
3:7: C1801: Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty

Why bother?

Using len(items) as a boolean makes the code unobvious. If items is empty, length will be zero which coerces to False. Just using items as the condition achieves the same and is - especially when you have good names - easier to read and understand.

def example():
    items = [1]
    if items:
        pass
No issues found.

Running Pylint locally? In your CI?

There is a better way! Run PyCodeQual & get Pylint findings with history, diffs and statistics.

Click here to find out more.