diff options
author | Rahul Kumaresan <kayrahul@gmail.com> | 2020-05-18 01:32:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-18 01:32:34 (GMT) |
commit | eefd4e033334a2a1d3929d0f7978469e5b5c4e56 (patch) | |
tree | 6120745cc0c7611c0aa677c4680e2f3e7cdd1f08 /Doc/tutorial | |
parent | 65460565df99fbda6a74b6bb4bf99affaaf8bd95 (diff) | |
download | cpython-eefd4e033334a2a1d3929d0f7978469e5b5c4e56.zip cpython-eefd4e033334a2a1d3929d0f7978469e5b5c4e56.tar.gz cpython-eefd4e033334a2a1d3929d0f7978469e5b5c4e56.tar.bz2 |
bpo-39705 : sorted() tutorial example under looping techniques improved (GH-18999)
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/datastructures.rst | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst index 0edb73a..ff4c797 100644 --- a/Doc/tutorial/datastructures.rst +++ b/Doc/tutorial/datastructures.rst @@ -614,6 +614,21 @@ 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) ... |