summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2012-11-17 10:02:30 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2012-11-17 10:02:30 (GMT)
commit9236a4e99d6a2d2e7402510db253cd5bc5a566f3 (patch)
tree825c52faaae37425f5f678f283712e825f125434 /Doc
parentf34e4de3f5f4ae1c695773457349386722ae0719 (diff)
downloadcpython-9236a4e99d6a2d2e7402510db253cd5bc5a566f3.zip
cpython-9236a4e99d6a2d2e7402510db253cd5bc5a566f3.tar.gz
cpython-9236a4e99d6a2d2e7402510db253cd5bc5a566f3.tar.bz2
#16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tutorial/datastructures.rst27
1 files changed, 19 insertions, 8 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index a2edca1..e6786cc 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -229,6 +229,7 @@ Don't use this example's definition of :func:`sum`: since summing numbers is
such a common need, a built-in function ``sum(sequence)`` is already provided,
and works exactly like this.
+.. _tut-listcomps:
List Comprehensions
-------------------
@@ -485,6 +486,10 @@ with no duplicate elements. Basic uses include membership testing and
eliminating duplicate entries. Set objects also support mathematical operations
like union, intersection, difference, and symmetric difference.
+Curly braces or the :func:`set` function can be used to create sets. Note: to
+create an empty set you have to use ``set()``, not ``{}``; the latter creates an
+empty dictionary, a data structure that we discuss in the next section.
+
Here is a brief demonstration::
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
@@ -511,6 +516,13 @@ Here is a brief demonstration::
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
+Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
+are also supported::
+
+ >>> a = {x for x in 'abracadabra' if x not in 'abc'}
+ >>> a
+ set(['r', 'd'])
+
.. _tut-dictionaries:
@@ -562,18 +574,17 @@ Here is a small example using a dictionary::
>>> 'guido' in tel
True
-The :func:`dict` constructor builds dictionaries directly from lists of
-key-value pairs stored as tuples. When the pairs form a pattern, list
-comprehensions can compactly specify the key-value list. ::
+The :func:`dict` constructor builds dictionaries directly from sequences of
+key-value pairs::
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
- >>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension
- {2: 4, 4: 16, 6: 36}
-Later in the tutorial, we will learn about Generator Expressions which are even
-better suited for the task of supplying key-values pairs to the :func:`dict`
-constructor.
+In addition, dict comprehensions can be used to create dictionaries from
+arbitrary key and value expressions::
+
+ >>> {x: x**2 for x in (2, 4, 6)}
+ {2: 4, 4: 16, 6: 36}
When the keys are simple strings, it is sometimes easier to specify pairs using
keyword arguments::