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

Rule suggestion: recommend dict.fromkeys over comprehension #9592

Closed
tjkuson opened this issue Jan 20, 2024 · 9 comments
Closed

Rule suggestion: recommend dict.fromkeys over comprehension #9592

tjkuson opened this issue Jan 20, 2024 · 9 comments
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule

Comments

@tjkuson
Copy link
Contributor

tjkuson commented Jan 20, 2024

Replace

{elt: None for elt in foo}

with

dict.fromkeys(foo)

dict.fromkeys is more efficient and readable.

In [4]: import random

In [5]: choices = ("red", "green", "blue")

In [6]: dupes = [random.choice(choices) for _ in range(1000)]

In [7]: %timeit list({elt: None for elt in dupes})
18.8 µs ± 97.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [8]: %timeit list(dict.fromkeys(dupes))
12.6 µs ± 49.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Efficiency boost tested on both Python 3.9 and 3.12.

Note: would have to skip this rule for mutable values (see #4613).

@charliermarsh
Copy link
Member

Looks great!

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule accepted Ready for implementation labels Jan 20, 2024
@mikeleppane
Copy link
Contributor

I could pick up this one?

@charliermarsh
Copy link
Member

Feel free unless @tjkuson planned to do it or had started on it -- I'll leave it to @tjkuson to confirm!

@tjkuson
Copy link
Contributor Author

tjkuson commented Jan 20, 2024

Feel free unless @tjkuson planned to do it or had started on it -- I'll leave it to @tjkuson to confirm!

Haven't started anything, have at it! @mikeleppane

@Skylion007
Copy link
Contributor

This would be a great rule for refurb, C4, or perflint. You should consider pinging the author of those libraries as well.

@bswck
Copy link
Contributor

bswck commented Jan 21, 2024

This applies not only to None, but any constant.
In my opinion, {elt: 0 for elt in foo} should also be replaced with dict.fromkeys(foo, 0).

@bswck
Copy link
Contributor

bswck commented Jan 21, 2024

@tjkuson @charliermarsh would you agree? ↑

@tjkuson
Copy link
Contributor Author

tjkuson commented Jan 21, 2024

@tjkuson would you agree? ↑

Yes, this was my intention with the issue. Admittedly, I communicated that poorly by using only examples with None. (I mostly use dict.fromkeys in the context of ordered deduplication.)

charliermarsh pushed a commit that referenced this issue Jan 24, 2024
…025`) (#9613)

## Summary

Checks for unnecessary `dict` comprehension when creating a new
dictionary from iterable. Suggest to replace with
`dict.fromkeys(iterable)`

See: #9592

## Test Plan

```bash
cargo test
```
@charliermarsh
Copy link
Member

Closed by #9613. Thanks @mikeleppane!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

5 participants