3

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] ]
divibisan
12.2k11 gold badges44 silver badges63 bronze badges
asked Feb 14, 2016 at 23:55
5
  • [ [7,2] , [[2,3],4] , [[[5,6],7],9] ] has a missing closing bracket. Commented Feb 14, 2016 at 23:57
  • 1
    A list is one level. A list in a list is two levels. A list in a list in a list is three. And so on... Commented Feb 14, 2016 at 23:59
  • Start with a 0. Go through the representation of the list from left to right. For a [ add 1, for a ] subtract 1. The highest number you reach ist the level. If you end up with something else than 0, then there is something wrong. Commented Feb 15, 2016 at 3:35
  • @Helloimjs0n is your question answered? Are there still things unclear? Commented Feb 19, 2016 at 23:44
  • @timgeb Yes! Thank you! Commented Feb 20, 2016 at 1:08

4 Answers 4

4

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.

answered Feb 15, 2016 at 0:07
1
  • Of course, a wonderful way to blow up this naive function is to pass it a recursive list. a = []; a.insert(0, a); nestprint(a) Commented Feb 15, 2016 at 5:50
0

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

answered Feb 15, 2016 at 0:04
0

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.

answered Feb 15, 2016 at 0:04
0

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.

answered Feb 15, 2016 at 0:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.