How to create nested list in Python
In this article, we will see how to create a nested list in Python.
A Nested list is simply a list inside another list. A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list.
A nested list is created by placing a comma-separated sequence of sublists.
li = [1, 2, ['red', [1, 2, 3], 'blue'], 4, [5]]
You can access individual items in a nested list using multiple indexes.
li = [1, 2, ['red', [1, 2, 3], 'blue'], 4, [5]] print(li[2]) print(li[2][1]) print(li[2][1][2])
Output:
['red', [1, 2, 3], 'blue'] [1, 2, 3] 3
Subscribe
Login
Please login to comment
0 Discussion