Question: 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](https://stackoverflow.com/questions/27411631/inline-for-loop), however, I'm still confused trying to understand the difference here.

Answer: 
you used Generator expressions instead of list comprehension
Generator Expressions are somewhat similar to list comprehensions, but the former doesn’t construct list object.
Instead of creating a list and keeping the whole sequence in the memory the generator generates the next element in demand.
The problem is here at `Data.append( elem for elem in get_children(child))`, you haven't wrapped the list comprehension. So the solution is `Data.append([elem for elem in get_children(child)])` to just add `[` at start and `]` at end of list comprehension.

I think yo are trying to get all child of the root, well here's solution from [Getting every child widget of a Tkinter window](https://stackoverflow.com/a/7290709/18002691)

```
root.winfo_children()

```

You can use this instead of `for-loop`, as this would provide you with every children of root window.