Join waitlist
PythonCommunity question

nested list comprehension order confuses me

Asked by Deepak Nair 1 month ago 1 answer 5 votes

[x for row in matrix for x in row]

which for runs first? I always get this wrong

1 answer

Neha Joshi1 month agoMost voted6

Same order as if you wrote normal loops:

for row in matrix:
    for x in row:
        result.append(x)

Just the `x` moves to the front. Conditions go at the end, same rule.

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.