How to Create a Nested List in Python?

Estimated read time 1 min read

To create a nested list in Python, you can simply create a list that contains other lists. Here’s an example:

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

In this example, nested_list is a list that contains three other lists. Each of the three inner lists contains three integers.

You can access individual elements in a nested list by specifying the index of the outer list and the index of the inner list. For example, to access the second element of the first inner list in nested_list, you can use the following code:

nested_list[0][1] # returns 2

In this code, nested_list[0] refers to the first inner list, and nested_list[0][1] refers to the second element in that list.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply