Steven D'Aprano <[email protected]> added the comment:
> 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.
----------
nosy: +steven.daprano
resolution: -> rejected
stage: -> resolved
status: open -> closed
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue35245>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com