[issue39705] Tutorial, 5.6 Looping Techniques, sorted() example

basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] for f in sorted(set(basket)): ... print(f) ... apple banana orange
New submission from Mirwi <mirwi@binary-kitchen.de>: 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. ---------- assignee: docs@python components: Documentation messages: 362395 nosy: docs@python, mirwi priority: normal severity: normal status: open title: Tutorial, 5.6 Looping Techniques, sorted() example type: enhancement versions: Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Eric V. Smith <eric@trueblade.com> added the comment: 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. ---------- nosy: +eric.smith _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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. ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Eric V. Smith <eric@trueblade.com> added the comment: That sounds like a good improvement, Raymond. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Rahul Kumaresan <kayrahul@gmail.com> added the comment: I would like to work on this documentation improvement task. Please help me understand if this is not being worked on already. ---------- nosy: +rahul-kumi _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Change by Rahul Kumaresan <kayrahul@gmail.com>: ---------- keywords: +patch pull_requests: +18346 stage: -> patch review pull_request: https://github.com/python/cpython/pull/18999 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset eefd4e033334a2a1d3929d0f7978469e5b5c4e56 by Rahul Kumaresan in branch 'master': bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999) https://github.com/python/cpython/commit/eefd4e033334a2a1d3929d0f7978469e5b5... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________

Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39705> _______________________________________
participants (4)
-
Eric V. Smith
-
Mirwi
-
Rahul Kumaresan
-
Raymond Hettinger