Skip to content

Commit 0c74847

Browse files
gh-137818: Add multiple if statements example for list comprehensions
1 parent 0dbbf61 commit 0c74847

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Doc/tutorial/datastructures.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ 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::
249+
250+
>>> [x for x in range(10) if x % 2 if x % 3]
251+
[1, 5, 7]
252+
253+
This example is equivalent to::
254+
255+
>>> [x for x in range(10) if x % 2 and x % 3]
256+
[1, 5, 7]
257+
258+
Note the second :keyword:`!if` is replaced by :keyword:`!and`.
259+
248260
If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
249261
it must be parenthesized. ::
250262

0 commit comments

Comments
 (0)