Alex
Alex

Reputation: 44415

How to fix pylint error "Unnecessary use of a comprehension"

With python 3.8.6 and pylint 2.4.4 the following code produces a pylint error (or recommendation)

R1721: Unnecessary use of a comprehension (unnecessary-comprehension)

This is the code:

dict1 = {
    "A": "This is A",
    "B": "This is B"
}
bools = [True, False]

dict2 = {key: value for key, value in zip(dict1.keys(), bools)}

How can I fix the code to get rid of this R1721 message?

Upvotes: 9

Views: 9798

Answers (1)

kaya3
kaya3

Reputation: 51162

The dict constructor takes an iterable of key/value pairs, so as the message says, a dict comprehension is unnecessary here.

dict2 = dict(zip(dict1.keys(), bools))

Upvotes: 8

Related Questions