We all know python's
[f(x) for x in y if g(x)]
syntax.
However the AST representation of list comprehension has room for more than one 'if' expression:
comprehension = (expr target, expr iter, expr* ifs)
Can somebody give me an example of python code that would produce an AST with more than one 'if' expression?
Just stack them after one another:
[i for i in range(100) if i > 10 if i < 50]
Produces the integers between 11 and 49, inclusive.
The grammar allows for multiple if statements because you can mix them between the for loops:
[j for i in range(100) if i > 10 for j in range(i) if j < 20]
The comprehension components should be viewed as nested statements, the above translates to:
lst = [] for i in range(100): if i > 10: for j in range(i): if j < 20: lst.append(j)
This also means that you can use multiple if
statements without for
loops in between:
[i for i in range(100) if i > 10 if i < 20]
Although non-sensical (just combine those using and
or with chained operators), it does translate to a legal nested set of statements still:
lst = [] for i in range(100): if i > 10: if i < 20: lst.append(i)
The grammar and parser do not specifically disallow such usage, in the same way that Python doesn't disallow you to nest if
statements.
Note that PEP 202 – List Comprehensions (the original proposal document that added this feature to the language) actually includes a double-if comprehension in the examples section:
>>> print [(i, f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1] [(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With