]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999)
authorRahul Kumaresan <kayrahul@gmail.com>
Mon, 18 May 2020 01:32:34 +0000 (07:02 +0530)
committerGitHub <noreply@github.com>
Mon, 18 May 2020 01:32:34 +0000 (18:32 -0700)
Doc/tutorial/datastructures.rst
Misc/NEWS.d/next/Documentation/2020-03-14-18-37-06.bpo-39705.nQVqig.rst [new file with mode: 0644]

index 0edb73ad736919dfe3e941e721b97d52ae9d0078..ff4c797f66cd63163457a5553cf813e63158c3e3 100644 (file)
@@ -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 (file)
index 0000000..3454b92
--- /dev/null
@@ -0,0 +1,2 @@
+Tutorial example for sorted() in the Loop Techniques section is given a better explanation.\r
+Also a new example is included to explain sorted()'s basic behavior.
\ No newline at end of file