Steven D'Aprano 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 t
New submission from Yechoh :
Suppose we have:
forest = [[1,2],[3,4]]
and we want:
l1 = [['1','2'],['3','4']]
we could write:
l1 = [[str(leaf) for leaf in tree] for tree in forest]
Now if we want:
l2 = ['1','2','3','4']
What I expect to need to write is:
l2 = [str(leaf) for leaf in tree for tree