Create a nested list object with an arbitrary depth

Create a nested list object with an arbitrary depth

Problem Description:

I want to create a nested list object. In this way, the user enters a positive integer, then add empty lists to the initial list, equal to the number entered by the user. The second list should be added to the first list, the third list should be added to the second list, the fourth list should be added to the third list, and so on.

How can I do this using Python?

Example in the picture:

Create a nested list object with an arbitrary depth

Solution – 1

a = []
for _ in range(x):
    a = [a]
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject