rspec/rules/S2835/python/rule.adoc
2021-04-28 16:49:39 +02:00

18 lines
592 B
Plaintext

There are several ways to create a new list based on the elements of some other collection, but the use of a list comprehension has multiple benefits. First, it is both concise and readable, and second, it yields a fully-formed object without requiring a mutable object as input that must be updated multiple times in the course of the list creation.
== Noncompliant Code Example
----
squares = []
for x in range(10):
squares.append(x**2) # Noncompliant
squares = map(lambda x: x**2, range(10)) #Noncompliant
----
== Compliant Solution
----
squares = [x**2 for x in range(10)]
----