Why this matters
Overly complex comprehensions can reduce readability and maintainability of the code. If a comprehension has multiple conditions or nested loops, consider using a regular loop for better clarity.
Ensure that list, set, or dictionary comprehensions are not overly complex. If a comprehension includes multiple conditions or nested loops, recommend refactoring to a regular loop for better readability.
Overly complex comprehensions can reduce readability and maintainability of the code. If a comprehension has multiple conditions or nested loops, consider using a regular loop for better clarity.
Side-by-side examples engineers can pattern-match during review.
result = [(x, y) for x in range(10) for y in range(5) if x * y > 10]result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
result = [(x, y) for x in range(10) for y in range(5) if x * y > 10]result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.