This rule raises an issue when list() calls are applied to types that are already directly iterable with for-loops or comprehensions.

Why is this an issue?

When iterating over an already iterable object with a for loop or a comprehension, wrapping it with list() adds meaningless clutter that doesn’t provide any functional value. Additionally, it creates unnecessary overhead by generating an intermediate list in memory, which inefficiently consumes memory and can degrade performance, especially with large data structures. Iterating directly over the original object is cleaner and more efficient.

How to fix it

Remove the redundant list() call and iterate directly over the original iterable.

Code examples

Noncompliant code example

some_iterable = range(10)
for i in list(some_iterable): # Noncompliant: unnecessary list() call
    print(i)

Compliant solution

some_iterable = range(10)
for i in some_iterable: # Compliant
    print(i)

Resources

Documentation