This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients Yechoh, steven.daprano
Date 2018-11-14.14:21:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1542205318.52.0.788709270274.issue35245@psf.upfronthosting.co.za>
In-reply-to
Content
> l2 = [str(leaf) for leaf in tree for tree in forest]

Expanded to nested loops, that becomes:

l2 = []
for leaf in tree:
    for tree in forest:
        l2.append(str(leaf))

which of course gives a NameError, because you are trying to iterate over a tree that you haven't defined yet.

The correct way to write this nested comprehension is to put the loops in the same order that they will appear in a nested loop:

[str(leaf) for tree in forest for leaf in tree]

There are thousands, or more, of nested comprehensions using the existing order in code around the world, if we swapped to your proposed "backwards" order it would change the meaning of their code and break it. That's not going to happen without an excellent reason.

I'm closing this "rejected". If you want to debate this and push for the proposed change, you should discuss it on the Python-Ideas mailing list first.
History
Date User Action Args
2018-11-14 14:21:58steven.dapranosetrecipients: + steven.daprano, Yechoh
2018-11-14 14:21:58steven.dapranosetmessageid: <1542205318.52.0.788709270274.issue35245@psf.upfronthosting.co.za>
2018-11-14 14:21:58steven.dapranolinkissue35245 messages
2018-11-14 14:21:58steven.dapranocreate