diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst index cbe780e075baf5..f25da6d58b911c 100644 --- a/Doc/tutorial/datastructures.rst +++ b/Doc/tutorial/datastructures.rst @@ -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. ::