Lesson 6
Dealing with Sequences of Objects
Chapter 2: Lists
Nested List
An item in a list
can be any object. It can even be another list
!!
nested_list = [1, 2, [3, 4, 5], 6]
super_nested_list = [1, 2, [3, 4, [5, 6, 7], 8], 9, [10, 11]]
Such nested lists can be used in many different applications. A prime example is for representing a table or a matrix. The elements in the ‘outer’ list represents each row, and the ‘inner’ list represents the columns in each row.
Accessing the elements inside a list is intuitive. The example below should be self-explanatory, I hope!
>>> x = [1, 2, [3, 4, 5], 6]
>>> print(len(x))
4
>>> print(x[2])
[3, 4, 5]
>>> print(len(x[2]))
3
>>> print(x[2][1])
4
Let’s check whether you really got this!
❮
❯