PY3.R1718

Use set comprehension

Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a set comprehension. Also it is faster since you don't need to create another transient list.

Noncompliant Code:

Copy
NUMBERS = [1, 2, 2, 3, 4, 4]

UNIQUE_EVEN_NUMBERS = set([number for number in NUMBERS if number % 2 == 0])

Compliant Code:

Copy
NUMBERS = [1, 2, 2, 3, 4, 4]

UNIQUE_EVEN_NUMBERS = {number for number in NUMBERS if number % 2 == 0}

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.