Why is Python’s list comprehension loop order backwards? [duplicate]

Consider:

[leaf for branch in tree for leaf in branch]

It unrolls like:

for branch in tree:
    for leaf in branch:
        yield leaf

If we write it the other way

[leaf for leaf in branch for branch in tree]

It might make more sense in plain English on some level, but a possible counter-argument is that here the name “branch” is used without being (yet) defined.

Leave a Comment