Skip to content

Commit 1d8128a

Browse files
Update Doc/tutorial/datastructures.rst
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
1 parent 44db781 commit 1d8128a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Doc/tutorial/datastructures.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,32 @@ and it's equivalent to::
245245
Note how the order of the :keyword:`for` and :keyword:`if` statements is the
246246
same 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

253253
This 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

260275
If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
261276
it must be parenthesized. ::

0 commit comments

Comments
 (0)