File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -245,17 +245,32 @@ and it's equivalent to::
245245Note how the order of the :keyword: `for ` and :keyword: `if ` statements is the
246246same in both these snippets.
247247
248- For multiple :keyword: ` !if ` statements, like this ::
248+ Looking at another example ::
249249
250250 >>> [x for x in range(10) if x % 2 if x % 3]
251251 [1, 5, 7]
252252
253253This example is equivalent to::
254254
255- >>> [x for x in range(10) if x % 2 and x % 3]
255+ >>> result = []
256+ >>> for x in range(10):
257+ ... if x % 2:
258+ ... if x % 3:
259+ ... result.append(x)
260+ ...
261+ >>> result
262+ [1, 5, 7]
263+
264+ This example could be further simplified by combining the two :keyword:if statements::
265+
266+ >>> result = []
267+ >>> for x in range(10):
268+ ... if x % 2 and if x % 3:
269+ ... result.append(x)
270+ ...
271+ >>> result
256272 [1, 5, 7]
257273
258- Note the second :keyword: `!if ` is replaced by :keyword: `!and `.
259274
260275If the expression is a tuple (e.g. the ``(x, y) `` in the previous example),
261276it must be parenthesized. ::
You can’t perform that action at this time.
0 commit comments