Editor settings

General settings

Save settings

Learn to program with Python

Accessing Nested List Elements #

To access the data of a nested list in Python, you can use the square brackets operator [] and specify the index of the nested list that you want to access.

For example, consider the previous nested list:

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

Here's what you get when you try to access the first element of the list:

print(my_list[0]) # output: [1,2,3]

And to access the first element within the first element, you can simply add another set of square brackets, like so:

print(my_list[0][0]) # output: 1

Exercise #

  • Given a list [[1, 2, 3], [4, 5, 6], [7, 8, 9]], get the second element of the second element and assign it to a variable wanted_element
  • Make sure to use [] to get the element
  • Do not edit the first line of the code
Output will be displayed here