Helloimjs0n
Helloimjs0n

Reputation: 67

Understanding nested lists in Python

I am having a bit of trouble understanding what a one-level list, two-level list and a three-level list is. I want to just eye-ball it and know it is one or the other, but I am getting confused with the brackets.

What defines levels in a list?

Is this considered a three-level list?

​[ [[5,6],7], 9]

Or is this a three-level list?

​[ [7,2], [[2,3],4], [[[5,6],7],9] ]

Upvotes: 3

Views: 589

Answers (4)

Seekheart
Seekheart

Reputation: 1173

The best way to think about it is in the sense of containers.

In a one dimensional list/array think of it as a single box containing data. so for example

my_li = [1,2,3]

is a box containing values 1,2,3.

A two dimensional list is like a box inside another box. If you open the first box, you'll find many other boxes inside containing information.

my_li_two = [[1],[2],[3]]

is a box containing 3 boxes each holding a single value.

A third level or 3 dimensional list is just like the 2 dimensional list except now it's a big box containing boxes containing boxes of values.

my_li_three = [[[1,2]],[[3]]]

any extra layers or levels will just add more boxes to this analogy.

Upvotes: 0

timgeb
timgeb

Reputation: 78650

The concept of nested lists is not terribly complicated, it just means that you can have a list inside of a list. In that list, you could have another list, and so on.

The terms one-level, two-level or n-level list are not widely used, it is more common to use the term nesting level. So let's write a small algorithm to visualize nesting levels:

>>> def nestprint(lst, level=0):
...     print('{} is at nesting level {}'.format(lst, level))
...     for item in lst:
...         if isinstance(item, list):
...             nestprint(item, level+1)

For a given list, this will print out the nesting level of each list. Here is what it does for your examples:

>>> nestprint([[[5,6],7],9])
[[[5, 6], 7], 9] is at nesting level 0
[[5, 6], 7] is at nesting level 1
[5, 6] is at nesting level 2
>>>    
>>> nestprint([[7,2],[[2,3],4],[[[5,6],7],9]])
[[7, 2], [[2, 3], 4], [[[5, 6], 7], 9]] is at nesting level 0
[7, 2] is at nesting level 1
[[2, 3], 4] is at nesting level 1
[2, 3] is at nesting level 2
[[[5, 6], 7], 9] is at nesting level 1
[[5, 6], 7] is at nesting level 2
[5, 6] is at nesting level 3

Hopefully this clears things up for you.

Upvotes: 4

Ethan Furman
Ethan Furman

Reputation: 69031

When one talks about levels in some kind of container (such as list, tuple, or dict) one is talking about nesting. So a one-level list doesn't contain other lists in it:

one_level = [1, 2, 3, 4]

Once you have added a list into another list, you have a two-level list:

two_level[['a', 'b', 'c'], ['d', 'e', 'f']]

and so on and so forth:

three_level = [[1, 2, 3], [[4, 5], [6, 7]], [8, 9, 10]]

four_level = [[[['this', 'that']], 'these'], 'those']

As you can see, the order of the nesting doesn't matter, only the depth of the nesting.

Upvotes: 0

Merijndk
Merijndk

Reputation: 1693

A basic list in python:

[1, 2, 3, 4, 5 ];

A list inside a list:

[1, 2, [1, 2]];

A list inside a list inside a list:

[1, 2, [1, 2, [1, 2]]];

You can go on like that forever

Upvotes: 0

Related Questions