summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-10-01 01:26:24 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-10-01 01:26:24 (GMT)
commitbbf4ae51e454b64d3b9408b4b4626a52d7175743 (patch)
tree38bfdbf36311c864ba73efdfc44ecb2c69b8aff7 /Doc/tutorial
parent5a789f7eaf81c2908bfea42c7ac721800e08a8c8 (diff)
parent6bd68608ffb1a0366760a190864bb05c618559ab (diff)
downloadcpython-bbf4ae51e454b64d3b9408b4b4626a52d7175743.zip
cpython-bbf4ae51e454b64d3b9408b4b4626a52d7175743.tar.gz
cpython-bbf4ae51e454b64d3b9408b4b4626a52d7175743.tar.bz2
Merge #21739: mention subtle difference between loops and listcomps in tutorial.
Diffstat (limited to 'Doc/tutorial')
-rw-r--r--Doc/tutorial/datastructures.rst11
1 files changed, 8 insertions, 3 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index 5e95afa..6dc17aa 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -200,12 +200,17 @@ For example, assume we want to create a list of squares, like::
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
-We can obtain the same result with::
+Note that this creates (or overwrites) a variable named ``x`` that still exists
+after the loop completes. We can calculate the list of squares without any
+side effects using::
+
+ squares = list(map(lambda x: x**2, range(10)))
+
+or, equivalently::
squares = [x**2 for x in range(10)]
-This is also equivalent to ``squares = list(map(lambda x: x**2, range(10)))``,
-but it's more concise and readable.
+which is more concise and readable.
A list comprehension consists of brackets containing an expression followed
by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`