×

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 - Nested Lists

Last Updated : May 01, 2025

In Python, nested lists are lists that contain other lists as elements. In this chapter, we will learn how to create nested lists, access elements, update values, and perform various operations with the help of examples.

Creating a Nested List

A nested list is a list that contains other lists as its elements. You can create a nested list by placing one or more lists inside another list, using square brackets [].

Example

In this example, we are creating a nested list where each inner list contains city names from a specific continent.

# Creating a nested list of cities 
# grouped by continent
cities = [
    ["New York", "Los Angeles", "Chicago"],
    ["London", "Paris", "Berlin"],
    ["Tokyo", "Seoul", "Beijing"]
]

# Displaying the nested list
print(cities)

The output of the above code would be:

[['New York', 'Los Angeles', 'Chicago'], ['London', 'Paris', 'Berlin'], ['Tokyo', 'Seoul', 'Beijing']]

Accessing Elements in a Nested List

You can access elements in a nested list using multiple indices. The first index selects the inner list, and the second index selects the item within that list.

Example

In this example, we are accessing specific cities from the nested list using index notation.

# Creating a nested list of cities 
# grouped by continent
cities = [
    ["New York", "Los Angeles", "Chicago"],
    ["London", "Paris", "Berlin"],
    ["Tokyo", "Seoul", "Beijing"]
]

# Accessing elements from the nested list
print(cities[0][1])  # Outputs 'Los Angeles'
print(cities[2][0])  # Outputs 'Tokyo'

The output of the above code would be:

Los Angeles
Tokyo

Looping Through a Nested List Using Nested Loops

You can use nested loops to iterate over a nested list. The outer loop iterates over the outer list, while the inner loop iterates over each sublist.

Example

In this example, we are printing all cities in the nested list using nested for loops.

# Creating a nested list of cities 
# grouped by continent
cities = [
    ["New York", "Los Angeles", "Chicago"],
    ["London", "Paris", "Berlin"],
    ["Tokyo", "Seoul", "Beijing"]
]

# Looping through a nested list
for continent in cities:
    for city in continent:
        print(city)

The output of the above code would be:

New York
Los Angeles
Chicago
London
Paris
Berlin
Tokyo
Seoul
Beijing

Modifying Elements in a Nested List

You can change the value of any element in a nested list by using index notation just like with regular lists.

Example

In this example, we are updating the city name 'Beijing' to 'Shanghai' in the nested list.

# Creating a nested list of cities 
# grouped by continent
cities = [
    ["New York", "Los Angeles", "Chicago"],
    ["London", "Paris", "Berlin"],
    ["Tokyo", "Seoul", "Beijing"]
]

# Changing an element in a nested list
cities[2][2] = "Shanghai"

# Displaying the updated list
print(cities)

The output of the above code would be:

[['New York', 'Los Angeles', 'Chicago'], ['London', 'Paris', 'Berlin'], ['Tokyo', 'Seoul', 'Shanghai']]

Using List Comprehension with Nested Lists

List comprehension can be used to flatten a nested list or to apply operations to elements within inner lists.

Example

In this example, we are flattening the nested list into a single list using list comprehension.

# Creating a nested list of cities 
# grouped by continent
cities = [
    ["New York", "Los Angeles", "Chicago"],
    ["London", "Paris", "Berlin"],
    ["Tokyo", "Seoul", "Beijing"]
]

# Flattening the nested list
flat_cities = [city for continent in cities for city in continent]

# Displaying the flattened list
print(flat_cities)

The output of the above code would be:

['New York', 'Los Angeles', 'Chicago', 'London', 'Paris', 'Berlin', 'Tokyo', 'Seoul', 'Shanghai']

Exercise

Select the correct option to complete each statement about nested lists in Python.

  1. What is a nested list in Python?
  2. Given my_list = [[1, 2], [3, 4]], what is the output of my_list[1][0]?
  3. Which of the following is the correct way to access the second element of the first sublist in list = [[10, 20], [30, 40]]?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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