Access Elements in a Nested List in Python

To access elements in a nested list in Python, you need to use multiple indices. The first index retrieves the inner list, and the second index accesses the specific element within that inner list. In this tutorial, we will explore different ways to access elements in a nested list with examples.


Examples

1. Accessing an Element in a Nested List

In this example, we will consider a two level nested list, which has inner lists.

To retrieve an element from a nested list, use two indices: the first selects the inner list, and the second selects the element within that list.

</>
Copy
# Defining a nested list
matrix = [
    [1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9]
]

# Accessing element at row index 1 and column index 2
element = matrix[1][2]

# Printing the retrieved element
print("Accessed Element:", element)

In this example:

  • matrix is a nested list containing three sublists.
  • matrix[1] retrieves the second sublist: [4, 5, 6].
  • matrix[1][2] accesses the third element of this sublist, which is 6.

Output:

Accessed Element: 6

2. Accessing an Entire Inner List

To access an entire sublist from a nested list, use a single index.

</>
Copy
# Defining a nested list
data = [
    ["Alice", 25], 
    ["Bob", 30], 
    ["Charlie", 22]
]

# Accessing the second inner list
person = data[1]

# Printing the retrieved list
print("Accessed Inner List:", person)

In this example:

  • data is a nested list where each sublist represents a person with their name and age.
  • data[1] retrieves the second sublist: ["Bob", 30], which contains Bob’s information.

Output:

Accessed Inner List: ['Bob', 30]

3. Using a Loop to Access Nested List Elements

We can use a loop to iterate over the inner lists and access their elements.

</>
Copy
# Defining a nested list
grades = [
    [85, 90, 78],
    [88, 76, 92],
    [79, 84, 88]
]

# Iterating through the nested list
for i, student in enumerate(grades):
    print(f"Student {i + 1} Grades:", student)

In this example:

  • grades is a nested list where each inner list represents a student’s grades.
  • The enumerate() function helps keep track of student numbers.
  • Each inner list is printed separately as we loop through grades.

Output:

Student 1 Grades: [85, 90, 78]
Student 2 Grades: [88, 76, 92]
Student 3 Grades: [79, 84, 88]

4. Accessing Elements Using Negative Indexing

Python supports negative indexing, which allows accessing elements from the end of a list.

</>
Copy
# Defining a nested list
movies = [
    ["Inception", "Interstellar", "Dunkirk"],
    ["Titanic", "Avatar", "The Terminator"],
    ["The Matrix", "John Wick", "Speed"]
]

# Accessing the last element of the last sublist
last_movie = movies[-1][-1]

# Printing the retrieved element
print("Last Movie in List:", last_movie)

In this example:

  • movies is a nested list containing movie titles.
  • movies[-1] retrieves the last inner list: ["The Matrix", "John Wick", "Speed"].
  • movies[-1][-1] accesses the last element of this list, which is "Speed".

Output:

Last Movie in List: Speed

Conclusion

Accessing elements in a nested list in Python can be done using:

  1. Direct Indexing: Use two indices to access a specific element.
  2. Accessing Entire Inner Lists: Use a single index to retrieve a full sublist.
  3. Using Loops: Iterate through the nested list to process all elements.
  4. Negative Indexing: Retrieve elements from the end of a list.