I’ve run into a piece of code that reads:
queue = [(Xi, Xk) for Xi in csp.vars for Xk in csp.neighbors[Xi]]
Is this the equivalent of:
for Xi in csp.vars:
for Xk in csp.neighbors[Xi]:
queue.append((Xi, Xk))
or does this indicate something other than nested for statements?
As other already pointed out that’s exactly what it means.
I also find sometimes confusing using multiple for inside a list-comprehension/genexp, so I usually avoid them. When I use them I usually put every for in a different line, such as:
Or even indenting:
This makes it clear which is the inner for loop.
Also, list-comprehension can have an if expression for every for loop, so the complete syntax is something like:
Even though I hope you’ll never write something like that. The if expression at the end is fine, but mixing it with for makes all too cumbersome and not at all readable.