# Generate a nested list with a loop function \[solved\]

**URL:** <https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929>\
**Category:** Python Help\
**Created:** [December 12, 2023, 9:28am UTC](https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929 "2023-12-12T09:28:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpongeB0B](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/spongeb0b/32/13257_2.png) [@SpongeB0B](https://discuss.python.org/u/SpongeB0B)\
**Post date:** [December 12, 2023, 9:28am UTC](https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929/1 "2023-12-12T09:28:33Z")

</div>

Hi everyone,

I have entries in a DB that have a parent-child relation.  
In the DB each entry have a field with their parent id.

I would like to create a simple looping function that generate a nested list of those relation

**DB**

| id | parent | name |
| --- | --- | --- |
| 1 | None | John |
| 2 | 1 | David |
| 3 | 2 | Grunt |
| 4 | 1 | Bob |

  

Relations:

```auto
John
+---Bob
\---David
    \---Grunt

```

  
 I have already a simple function that return a list of tuples when given an id

```python
def returningchilds(Target_id):
   #omitted to simplify reading

returningchilds(1)

>>> [(2, 'David' ), (4, 'Bob')]

```

Then `David` have also a child and I would like to loop those too.

and have as final result a nested list

```python

[[1, 'John', [ [4, 'Bob', None], [2, 'David', [[ 3, 'Grunt'] ] ] ] ]

```

BTW  
the argument of `returningchilds()` can be changed and accept the same format has is output to simplify the loop.

Any ideas ?

Thanks.

---

<div class="post-metadata">

**Author:** ![SIvaCoHan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sivacohan/32/16060_2.png) [@SIvaCoHan](https://discuss.python.org/u/SIvaCoHan)\
**Post date:** [December 12, 2023, 9:55am UTC](https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929/2 "2023-12-12T09:55:05Z")

</div>

All you need is a method whether BFS or DFS  
BFS: Breadth-first search  
DFS: Depth-first search

and the implement is based on what your database is and which driver you use

---

<div class="post-metadata">

**Author:** ![WeisLeDocto](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/weisledocto/32/15827_2.png) [@WeisLeDocto](https://discuss.python.org/u/WeisLeDocto)\
**Post date:** [December 12, 2023, 10:40am UTC](https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929/3 "2023-12-12T10:40:13Z")

</div>

Hi !

For a quick and dirty solution re-using what you already have, you could try a recursive approach. The pseudo-code would be something like:

```auto
def returningchilds(Target_id):
    direct_children = ... # Uses the code you already have
    ret = list()
    for id_ in direct_children:
        children = returningchilds(id_)
        if children:
            ret.append([id_, children])
        else:
            ret.append([id_, None])
    return ret

```

Basically, it re-runs `returningchilds` for each detected direct child for the given id. Note that I use the `id_` variable in a generic way, it can represent just the index or the index + the name depending on the location in the code. Also note that it’s a quick-and-dirty solution, there would be tons ow ways to improve it and make it more robust.

I’m not familiar with database management in Python, but I assume there must be plenty of helpful libraries for this application. Maybe someone else will orient you towards such solutions.

---

<div class="post-metadata">

**Author:** ![SpongeB0B](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/spongeb0b/32/13257_2.png) [@SpongeB0B](https://discuss.python.org/u/SpongeB0B)\
**Post date:** [December 12, 2023, 11:48am UTC](https://discuss.python.org/t/generate-a-nested-list-with-a-loop-function-solved/40929/4 "2023-12-12T11:48:24Z")

</div>

Thank you very much @WeisLeDocto ,

Based on your example I’ve create a similar recursive function that give me the desired output 👍
