Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend to C416 to dict comprehensions #489

Closed
Skylion007 opened this issue Mar 15, 2023 · 2 comments · Fixed by #490
Closed

Extend to C416 to dict comprehensions #489

Skylion007 opened this issue Mar 15, 2023 · 2 comments · Fixed by #490

Comments

@Skylion007
Copy link
Contributor

Skylion007 commented Mar 15, 2023

Description

keys = range(100)
values = range(100)

out = {a:b for a,b in zip(keys, values)}

This is likely inefficient compared to the follow which does not need to unpack / repack the tuple.

out = dict(zip(keys, values))

This is essentially an extension of C416 to handle dict comprehensions as well. The version without the dict comprehension should be identical to the original code, just potentially more efficient.

@Skylion007 Skylion007 changed the title Unnecessary dict comprehension from zip iterable (extension to C416) Extend to C416 to dict comprehensions with zip Mar 15, 2023
@adamchainz
Copy link
Owner

Is zip required to be sure of this? Can't we suggest to rewrite more generally:

-{a: b for a, b in _}
+dict(_)

For any _ ? I think so...

Would you like to work on a PR here?

@Skylion007
Copy link
Contributor Author

@adamchainz Yep, any iterable would work. This was easier to add than I thought: #490

@Skylion007 Skylion007 changed the title Extend to C416 to dict comprehensions with zip Extend to C416 to dict comprehensions Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants