×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Iterate Over a List of Lists

Last Updated : May 03, 2025

In Python, a list can contain other lists. You can iterate through these lists using loops to access each sublist and its elements. In this chapter, we will learn how to iterate over a list of lists with the help of examples.

Iterate Over a List of Lists Using Nested Loops

You can use nested for loops to iterate through each sublist and its elements.

Example

In this example, we are iterating through a list of lists containing student names and their scores.

# Creating a list of lists
students_scores = [["Ananya", 85], ["Bharat", 78], ["Charu", 92]]

# Iterating using nested loops
for student in students_scores:
    for item in student:
        print(item, end=' ')
    print()

The output of the above code would be:

Ananya 85 
Bharat 78 
Charu 92 

Iterate Over a List of Lists Using Indexes

You can also access sublist elements using indexing if you know the structure of the list.

Example

In this example, we directly access student names and their scores using index positions.

# Creating a list of lists
students_scores = [["Ananya", 85], ["Bharat", 78], ["Charu", 92]]

# Accessing elements using indexes
for student in students_scores:
    name = student[0]
    score = student[1]
    print(f"{name} scored {score} marks")

The output of the above code would be:

Ananya scored 85 marks
Bharat scored 78 marks
Charu scored 92 marks

Iterate Over a List of Lists Using List Comprehension

You can use nested list comprehensions to flatten or process a list of lists.

Example

In this example, we are flattening a list of lists into a single list using list comprehension.

# Creating a list of lists
data = [[1, 2], [3, 4], [5, 6]]

# Flattening the list of lists
flattened = [item for sublist in data for item in sublist]

# Displaying the result
print(flattened)

The output of the above code would be:

[1, 2, 3, 4, 5, 6]

Iterate Over List of Lists with enumerate()

Using enumerate() lets you track the index of each sublist during iteration.

Example

In this example, we are printing each student's data along with their index in the outer list.

# Creating a list of lists
students_scores = [["Ananya", 85], ["Bharat", 78], ["Charu", 92]]

# Using enumerate to access index and sublist
for i, student in enumerate(students_scores):
    print(f"Student {i}: {student[0]}, Score: {student[1]}")

The output of the above code would be:

Student 0: Ananya, Score: 85
Student 1: Bharat, Score: 78
Student 2: Charu, Score: 92

Iterate Over a List of Lists Using Conditional Statements

You can apply conditions while iterating over a list of lists to filter or modify data.

Example

In this example, we only print students who scored more than 80 marks.

# Creating a list of lists
students_scores = [["Ananya", 85], ["Bharat", 78], ["Charu", 92]]

# Printing students with scores above 80
for name, score in students_scores:
    if score > 80:
        print(f"{name} passed with distinction")

The output of the above code would be:

Ananya passed with distinction
Charu passed with distinction

Exercise

Select the correct option to complete each statement about iterating over a list of lists in Python.

  1. To iterate over each inner list in a list of lists, use a ___ loop.
  2. To access individual elements in each sublist, use ___ loops.
  3. Given matrix = [[1, 2], [3, 4], [5, 6]], the value of matrix[2][1] is ___.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.