Title: Tutorial, 5.6 Looping Techniques, sorted() example
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.9
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, eric.smith, mirwi, rahul-kumi, rhettinger
Priority: normal Keywords: patch

Created on 2020-02-21 09:24 by mirwi, last changed 2020-05-18 01:32 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18999 merged rahul-kumi, 2020-03-14 18:23
Messages (6)
msg362395 - (view) Author: Mirwi (mirwi) Date: 2020-02-21 09:24
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
...     print(f)

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.
msg362398 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-02-21 09:35
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.
msg362415 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-02-21 17:00
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.
msg362422 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-02-21 18:22
That sounds like a good improvement, Raymond.
msg362650 - (view) Author: Rahul Kumaresan (rahul-kumi) * Date: 2020-02-25 17:04
I would like to work on this documentation improvement task.
Please help me understand if this is not being worked on already.
msg369158 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-18 01:32
New changeset eefd4e033334a2a1d3929d0f7978469e5b5c4e56 by Rahul Kumaresan in branch 'master':
bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999)
Date User Action Args
2020-05-18 01:32:59rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-05-18 01:32:41rhettingersetmessages: + msg369158
2020-03-14 18:23:43rahul-kumisetkeywords: + patch
stage: patch review
pull_requests: + pull_request18346
2020-02-25 17:04:00rahul-kumisetnosy: + rahul-kumi
messages: + msg362650
2020-02-21 18:22:02eric.smithsetmessages: + msg362422
2020-02-21 17:00:16rhettingersetassignee: docs@python -> rhettinger

messages: + msg362415
nosy: + rhettinger
2020-02-21 09:35:41eric.smithsetnosy: + eric.smith
messages: + msg362398
2020-02-21 09:24:31mirwicreate