PY3.R1728
Use generator
If your container can be large using a generator will bring better performance.
Noncompliant Code:
Copy
list([0 for y in list(range(10))])
tuple([0 for y in list(range(10))])
sum([y**2 for y in list(range(10))])
max([y**2 for y in list(range(10))])
min([y**2 for y in list(range(10))])
Compliant Code:
Copy
list(0 for y in list(range(10)))
tuple(0 for y in list(range(10)))
sum(y**2 for y in list(range(10)))
max(y**2 for y in list(range(10)))
min(y**2 for y in list(range(10)))
Additional details:
Removing [] inside calls that can use containers or generators should be considered for performance reasons since a generator will have an upfront cost to pay. The performance will be better if you are working with long lists or sets. For max, min and sum using a generator is also recommended by pep289.