Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested List | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set
5. For deleting

bookNested List

Items within a list in Python can also be lists. This forms what is called a "nested list" or "lists within lists." Nested lists are powerful in organizing elaborative data structures in that you can store multiple-level data within one list.

For instance, you might have a list where some elements are just single values, and others might be lists themselves. Here's a simple example:

123
cities = ["London", ["Paris", "Madrid"], "Rome", ["Bangkok", ["New York", "Los Angeles"]]] print(cities)
copy

Using Variables for Nested Lists

This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.

You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:

1234567
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
copy

Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.

The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.

Task

Construct a list called list_2 containing these elements:

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

bookNested List

Items within a list in Python can also be lists. This forms what is called a "nested list" or "lists within lists." Nested lists are powerful in organizing elaborative data structures in that you can store multiple-level data within one list.

For instance, you might have a list where some elements are just single values, and others might be lists themselves. Here's a simple example:

123
cities = ["London", ["Paris", "Madrid"], "Rome", ["Bangkok", ["New York", "Los Angeles"]]] print(cities)
copy

Using Variables for Nested Lists

This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.

You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:

1234567
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
copy

Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.

The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.

Task

Construct a list called list_2 containing these elements:

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

bookNested List

Items within a list in Python can also be lists. This forms what is called a "nested list" or "lists within lists." Nested lists are powerful in organizing elaborative data structures in that you can store multiple-level data within one list.

For instance, you might have a list where some elements are just single values, and others might be lists themselves. Here's a simple example:

123
cities = ["London", ["Paris", "Madrid"], "Rome", ["Bangkok", ["New York", "Los Angeles"]]] print(cities)
copy

Using Variables for Nested Lists

This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.

You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:

1234567
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
copy

Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.

The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.

Task

Construct a list called list_2 containing these elements:

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Items within a list in Python can also be lists. This forms what is called a "nested list" or "lists within lists." Nested lists are powerful in organizing elaborative data structures in that you can store multiple-level data within one list.

For instance, you might have a list where some elements are just single values, and others might be lists themselves. Here's a simple example:

123
cities = ["London", ["Paris", "Madrid"], "Rome", ["Bangkok", ["New York", "Los Angeles"]]] print(cities)
copy

Using Variables for Nested Lists

This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.

You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:

1234567
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
copy

Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.

The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.

Task

Construct a list called list_2 containing these elements:

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt