Merge #21739: mention subtle difference between loops and listcomps in tutorial.

This commit is contained in:
R David Murray 2014-09-30 21:26:24 -04:00
commit bbf4ae51e4
1 changed files with 8 additions and 3 deletions

View File

@ -200,12 +200,17 @@ For example, assume we want to create a list of squares, like::
>>> squares >>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] [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)] squares = [x**2 for x in range(10)]
This is also equivalent to ``squares = list(map(lambda x: x**2, range(10)))``, which is more concise and readable.
but it's more concise and readable.
A list comprehension consists of brackets containing an expression followed A list comprehension consists of brackets containing an expression followed
by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if` by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`