Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Doc/tutorial/datastructures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@ and it's equivalent to::
Note how the order of the :keyword:`for` and :keyword:`if` statements is the
same in both these snippets.

Looking at another example::

>>> [x for x in range(10) if x % 2 if x % 3]
[1, 5, 7]

This example is equivalent to::

>>> result = []
>>> for x in range(10):
... if x % 2:
... if x % 3:
... result.append(x)
...
>>> result
[1, 5, 7]

This example could be further simplified by combining the two :keyword:if statements::

>>> result = []
>>> for x in range(10):
... if x % 2 and if x % 3:
... result.append(x)
...
>>> result
[1, 5, 7]


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

Expand Down
Loading