Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbf4ae5

Browse files
committed
Merge #21739: mention subtle difference between loops and listcomps in tutorial.
2 parents 5a789f7 + 6bd6860 commit bbf4ae5

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

Doc/tutorial/datastructures.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,17 @@ For example, assume we want to create a list of squares, like::
200200
>>> squares
201201
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
202202

203-
We can obtain the same result with::
203+
Note that this creates (or overwrites) a variable named ``x`` that still exists
204+
after the loop completes. We can calculate the list of squares without any
205+
side effects using::
206+
207+
squares = list(map(lambda x: x**2, range(10)))
208+
209+
or, equivalently::
204210

205211
squares = [x**2 for x in range(10)]
206212

207-
This is also equivalent to ``squares = list(map(lambda x: x**2, range(10)))``,
208-
but it's more concise and readable.
213+
which is more concise and readable.
209214

210215
A list comprehension consists of brackets containing an expression followed
211216
by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`

0 commit comments

Comments
 (0)