-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Tutorial, 5.6 Looping Techniques, sorted() example #83886
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
Comments
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear Shouldn't 'apple' appear two times as basket is a list that allows duplicates, not a set? I'm just doing my first steps into Python and may be mislead. In that case, sorry for the fuzz. |
The code is converting to a set first, then calls sorted() on that set. So "apple" is removed when the set is created. I'm not sure the example should throw in creating a set while it's talking about sorting. |
I prefer to keep the example as-is. It is an idiomatic way to loop over sets. The introductory text can be modified to explain that sets eliminate duplicates, that sets are ordered, and that sorted() puts them back in a deterministic order. |
That sounds like a good improvement, Raymond. |
I would like to work on this documentation improvement task. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: