-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Get the first item from an iterable #5503
Comments
This is not only faster but would consume less memory as it doesn't need to create the entire list to access the only element :) |
You can assign this to me. I'll add it as a |
This looks reasonable, but let's be really careful in reviewing any violations that pop up in the ecosystem CI. |
@charliermarsh Yup! |
charliermarsh
pushed a commit
that referenced
this issue
Jul 10, 2023
## Summary Fixes #5503. Ready for final review as the `mkdocs` issue involving SSH keys is fixed. Note that this will only throw on a `Name` - it will be refactorable once we have a type-checker. This means that this is the only sort of input that will throw. ```python x = range(10) list(x)[0] ``` I thought it'd be confusing if we supported direct function results. Consider this example, assuming we support direct results: ```python # throws list(range(10))[0] def createRange(bound): return range(bound) # "why doesn't this throw, but a direct `range(10)` call does?" list(createRange(10))[0] ``` If it's necessary, I can go through the list of built-ins and find those which produce iterables, then add them to the throwing list. ## Test Plan Added a new fixture, then ran `cargo t`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes I need to get the first item of an iterable like a
dict_keys()
object. A natural way to do this is to do it withlist(...)[0]
, though it can be much faster to do it withnext(iter(...))
.I have tried to see if a rule already describes this, but I could not find it.
Some examples:
Code
The text was updated successfully, but these errors were encountered: