[Python-checkins] bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999)

Rahul Kumaresan webhook-mailer at python.org
Sun May 17 21:32:42 EDT 2020


https://github.com/python/cpython/commit/eefd4e033334a2a1d3929d0f7978469e5b5c4e56
commit: eefd4e033334a2a1d3929d0f7978469e5b5c4e56
branch: master
author: Rahul Kumaresan <kayrahul at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-05-17T18:32:34-07:00
summary:

bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999)

files:
A Misc/NEWS.d/next/Documentation/2020-03-14-18-37-06.bpo-39705.nQVqig.rst
M Doc/tutorial/datastructures.rst

diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index 0edb73ad73691..ff4c797f66cd6 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -613,6 +613,21 @@ direction and then call the :func:`reversed` function. ::
 To loop over a sequence in sorted order, use the :func:`sorted` function which
 returns a new sorted list while leaving the source unaltered. ::
 
+   >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
+   >>> for i in sorted(basket):
+   ...     print(i)
+   ...
+   apple
+   apple
+   banana
+   orange
+   orange
+   pear
+
+Using :func:`set` on a sequence eliminates duplicate elements. The use of
+:func:`sorted` in combination with :func:`set` over a sequence is an idiomatic
+way to loop over unique elements of the sequence in sorted order. ::
+
    >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
    >>> for f in sorted(set(basket)):
    ...     print(f)
diff --git a/Misc/NEWS.d/next/Documentation/2020-03-14-18-37-06.bpo-39705.nQVqig.rst b/Misc/NEWS.d/next/Documentation/2020-03-14-18-37-06.bpo-39705.nQVqig.rst
new file mode 100644
index 0000000000000..3454b928e70b4
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2020-03-14-18-37-06.bpo-39705.nQVqig.rst
@@ -0,0 +1,2 @@
+Tutorial example for sorted() in the Loop Techniques section is given a better explanation.
+Also a new example is included to explain sorted()'s basic behavior.
\ No newline at end of file



More information about the Python-checkins mailing list