Skip to main content
ai-slop/python-range-len-loopmanual fix

range(len()) loop.

`for i in range(len(items)):` is a C-style index loop where Python offers direct iteration or `enumerate`.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

python-range-len-loop.py
✗ Flagged
names = []
for i in range(len(users)):
    names.append(users[i].name)
Why this trips

Deterministic line match: flags `for name in range(len(seq)):` where the argument is exactly `len()` of a name or dotted attribute. Reported at info severity.

python-range-len-loop.py
✓ Clean
names = [user.name for user in users]

for position, user in enumerate(users, start=1):
    print_row(position, user.name)
What changed

Iterate directly (`for item in items`), use `enumerate(items)` when you need the index too, and `zip` when walking two sequences together.

Agents carry index-loop habits over from other languages and keep the index plumbing even when the body only reads `items[i]`.

How this rule is justified.

Rule id

ai-slop/python-range-len-loop

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: flags `for name in range(len(seq)):` where the argument is exactly `len()` of a name or dotted attribute. Reported at info severity.

Legitimate code that is NOT flagged

Direct iteration, `enumerate(...)`, and ranges with offsets or steps such as `range(1, len(x))` or `range(len(x) - 1)` are not flagged.

Evidence

Shipped in aislop v0.9.4 as one of four Python verbosity rules drawn from SlopCodeBench (arXiv 2603.24755).