R1721 unnecessary-comprehension

Message

'Unnecessary use of a comprehension'

Description

Instead of using an identitiy comprehension, consider using the list, dict or set constructor. It is faster and simpler.

Examples

The comprehension in the following example does not change the contents of the list.

def example(arg):
    lst = [1,2,3]
    other_lst = [elem for elem in lst]
3:0: R1721: Unnecessary use of a comprehension

This kind of comprehension is usually applied to ensure that a iterable of any type is normalized to a list. The message description suggest to use the list constructor instead:

def example(arg):
    lst = (1,2,3)
    other_lst = list(lst)
No issues found.

Running Pylint locally? In your CI?

There is a better way! Run PyCodeQual & get Pylint findings with history, diffs and statistics.

Click here to find out more.