I can ask my question best by just giving an example. Let’s say I want to use a list comprehension to generate a set of 3-element tuples from two loops, something like this:
[ (y+z,y,z) for y in range(10) if y%2==0 for z in range(20) if z%3==0 ]
This works, giving me
[(0, 0, 0), (3, 0, 3), (6, 0, 6), (9, 0, 9), (12, 0, 12), (15, 0, 15), ... ]
I am wondering, though, if there is a way to do it more cleanly, something to the effect of
[ (x,y,z) for y in range(10) if y%2==0 for z in range(20) if z%3==0 ... somehow defining x(y,z) ... ]
I would consider something like this to be more clean, especially since what I really need to do is much more complicated than the example I give here. Everything I have tried has given me a syntax error.
1
You can do:
out = [
(x, y, z)
for y in range(10)
if y % 2 == 0
for z in range(20)
if z % 3 == 0
for x in [y + z] # <-- initialize `x` in list-comprehension
]
This is optimized since Python 3.9: https://docs.python.org/3/whatsnew/3.9.html#optimizations
8
I would skip the division and just generate the desired multiples explicitly.
[(y+z,y,z) for y in range(0, 10, 2) for z in range(0, 20, 3)]
Now you can use itertools.product
instead of two generators.
[(sum(p), *p) for p in product(range(0, 10, 2), range(0, 20, 3))]
There is no reason to do this in the scenario described no matter how complex the expression being named is. If the expression is complicated, just break up the tuple
definition:
[(y+z, # Doesn't matter if this is complex; by using one element per line, it's clear
# the complexity doesn't interfere with interpreting the rest of the listcomp
y,
z)
for y in range(10)
if y%2==0
for z in range(20)
if z%3==0]
If you actually both test and produce the value (so it must be assigned to a name), sure, use Andrej’s solution, and do:
[(x, y, z) # Produce here, having computed x only once
for y in range(10)
if y%2==0
for z in range(20)
if z%3==0
for x in [y+z] # Substitute complex expression here
if x%5==0 # Test here
]
or if you don’t care about x
leaking from the listcomp’s scope, the even simpler/more concise walrus-based (:=
) approach:
[(x, y, z) # Produce here, having computed x only once
for y in range(10)
if y%2==0
for z in range(20)
if z%3==0 and (x := y+z)%5==0 # Test and assign to x here
]
But when you only need the value once, there’s not much to be gained from naming it unless you have a really good name to give it for clarity (and I’d argue that defining a simple named tuple for the produced values might be the way to go in that case, rather than only giving it a useful name inside the listcomp).
If the expression is complex enough to justify more than this, it’s complex enough that you should probably just drop the listcomp in favor of a manual expansion of it to a for loop, e.g.:
result = []
for y in range(10):
if y%2 == 0:
for z in range(20):
if z%3 == 0:
x = y + z # You can do the complex expression here, possibly breaking it up
# into multiple lines itself for clarity
result.append((x, y, z))
or factoring it out to a generator function:
def do_whatever():
for y in range(10):
if y%2 == 0:
for z in range(20):
if z%3 == 0:
x = y + z # You can do the complex expression here, possibly breaking it up
# into multiple lines itself for clarity
yield (x, y, z)
that you can invoke with do_whatever()
(list(do_whatever())
to collect the results to a list
immediately if you can’t just iterate them one by one for processing).