I have a tree from which I want to return its children as a list through a function. In this function, I needed to iterate over a list of strings. At first I did it by using the inline for loop
def get_children(root):
Data = [ ]
Data.append( root.data )
for child in root.children:
Data.append( elem for elem in get_children(child) )
return Data
However, I got the following strange result:
['Electronics', <generator object get_children.<locals>.<genexpr> at 0x7fb3a081a3c0>, <generator object get_children.<locals>.<genexpr> at 0x7fb3a081a7b0>, <generator object get_children.<locals>.<genexpr> at 0x7fb3a081a9e0>]
Then, I changed the inline for loop into a typical one and the problem is resolved, i.e.,
for elem in get_children(child):
Data.append( elem )
I was wondering if you could help me understand why this happened. I've read a similar post about list comprehension, however, I'm still confused trying to understand the difference here.