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.

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.