Fiveable

🐍Intro to Python Programming Unit 9 Review

QR code for Intro to Python Programming practice questions

9.4 Nested lists

9.4 Nested lists

Written by the Fiveable Content Team • Last updated June 2026
Written by the Fiveable Content Team • Last updated June 2026
🐍Intro to Python Programming
Unit & Topic Study Guides

Nested lists in Python let you organize data inside lists within lists, which is useful for matrices, tables, and other multi-dimensional structures. You can access and change that data with multiple indexes, slices, and standard list methods.

Traversing nested lists often uses nested loops: the outer loop moves through the main list, while the inner loop handles each inner list. That pattern lets you process and modify data across rows, columns, or grouped values.

Nested Lists

Nested lists for complex data

  • Nested lists contain other lists as elements enabling organization of hierarchical or multi-dimensional data (matrices, tables, tree-like structures)
  • Create nested lists by enclosing lists within square brackets [] and separating elements with commas
    • nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  • Manipulate nested lists by:
    1. Appending lists to an existing nested list using append() method
    2. Extending nested lists with elements from another list using extend() method
    3. Inserting lists at a specific position using insert() method
    4. Removing lists from a nested list using remove() method or del statement
  • Nested lists are a common form of nested data structures
Nested lists for complex data, Python Data Structures

Multi-dimensional indexing in lists

  • Access elements in nested lists using multiple square brackets [] at different levels of nesting
    • nested_list[0][1] accesses the second element of the first inner list
  • Modify elements in nested lists by assigning new values using multi-dimensional indexing
    • nested_list[1][2] = 10 changes the third element of the second inner list to 10
  • Extract portions of nested lists using slicing with multiple square brackets
    • nested_list[0:2][1] returns the second inner list from the first two inner lists
  • Nested lists can be used to represent two-dimensional arrays
Nested lists for complex data, Python Data Structures

Nested loops for data traversal

  • Traverse nested lists using nested loops with outer loops iterating over the main list and inner loops iterating over the inner lists
    </>Python
    for inner_list in nested_list:
        for element in inner_list:
            print(element)
  • Process data in nested lists by performing operations or calculations on elements using nested loops
    • Sum all elements in a nested list
      </>Python
      total = 0
      for inner_list in nested_list:
          for element in inner_list:
              total += element
  • Modify data in nested lists using nested loops and conditional statements
    • Double even numbers in a nested list
      </>Python
      for i in range(len(nested_list)):
          for j in range(len(nested_list[i])):
              if nested_list[i][j] % 2 == 0:
                  nested_list[i][j] *= 2
  • Nested loops are useful for iteration through complex nested structures

Working with Lists of Lists

  • A list of lists is a common way to represent tabular or grid-like data
  • Nested loops are often used to process lists of lists efficiently
  • Two-dimensional arrays can be implemented using lists of lists in Python
  • Iteration through lists of lists requires careful consideration of the nested structure
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →