Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested Lists | Other Data Types
Introduction to Python Video Course
course content

Course Content

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

Nested Lists

A nested list in Python is a list that contains other sublists as its elements. This structure is particularly useful for grouping related items within a main list, where each sublist often shares common attributes or relationships.

To access elements within these sublists, indexing is used sequentially — meaning, we select the main list index first and then the sublist index. The following practical application and diagram provide a detailed look at how you can create and manage nested lists effectively.

Example Application

A customer in your grocery store has compiled a list of grocery items, where each item's details are stored in sublists under variable names.

We will help the customer access details from the milk sublist, add a new item, remove an existing item, and sort the entire grocery_list.

Let's start by creating a list first, and go step by step:

12345678910111213141516171819202122232425
# Define individual grocery items as lists containing details bread = ["Bread", 4.80, 3, "Gluten Free"] # Item name, price, quantity, type milk = ["Milk", 5.99, 2, "2% Milk"] # Item name, price, quantity, type apple = ["Apple", 1.27, 12, "Fuji"] # Item name, price, quantity, type # Create the main grocery list that contains these items grocery_list = [bread, apple, milk] print("Grocery List:" , grocery_list) # Accessing and printing specific item details using indexing print("Item:", grocery_list[2][0]) # Accesses "Milk" title print("Price:", grocery_list[2][1]) # Accesses price of a Milk, which is 5.99 print("Quantity:", grocery_list[2][2]) # Accesses quantity of Milk, which is 2 print("Type:", grocery_list[2][3]) # Accesses type of Milk, which is "2% Milk" # Adding a new sublist item to the grocery list onion = ["Onions", 1.30, 10, "Yellow"] grocery_list.append(onion) # Removing an item from the grocery list grocery_list.remove(bread) # Sorting the grocery list alphabetically grocery_list.sort() print("Updated Grocery List:", grocery_list)
copy

The diagram below illustrates the nested list structure of grocery_list. Each item in the list, such as milk, apple, and bread, is itself a list containing a specific item details.

For instance, to access the price of milk, which is stored in the milk sublist, you use the syntax grocery_list[2][1]. Here, grocery_list[2] selects the milk sublist, and grocery_list[2][1] goes a step further to access the second element of that sublist — the price.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 2
toggle bottom row

Nested Lists

A nested list in Python is a list that contains other sublists as its elements. This structure is particularly useful for grouping related items within a main list, where each sublist often shares common attributes or relationships.

To access elements within these sublists, indexing is used sequentially — meaning, we select the main list index first and then the sublist index. The following practical application and diagram provide a detailed look at how you can create and manage nested lists effectively.

Example Application

A customer in your grocery store has compiled a list of grocery items, where each item's details are stored in sublists under variable names.

We will help the customer access details from the milk sublist, add a new item, remove an existing item, and sort the entire grocery_list.

Let's start by creating a list first, and go step by step:

12345678910111213141516171819202122232425
# Define individual grocery items as lists containing details bread = ["Bread", 4.80, 3, "Gluten Free"] # Item name, price, quantity, type milk = ["Milk", 5.99, 2, "2% Milk"] # Item name, price, quantity, type apple = ["Apple", 1.27, 12, "Fuji"] # Item name, price, quantity, type # Create the main grocery list that contains these items grocery_list = [bread, apple, milk] print("Grocery List:" , grocery_list) # Accessing and printing specific item details using indexing print("Item:", grocery_list[2][0]) # Accesses "Milk" title print("Price:", grocery_list[2][1]) # Accesses price of a Milk, which is 5.99 print("Quantity:", grocery_list[2][2]) # Accesses quantity of Milk, which is 2 print("Type:", grocery_list[2][3]) # Accesses type of Milk, which is "2% Milk" # Adding a new sublist item to the grocery list onion = ["Onions", 1.30, 10, "Yellow"] grocery_list.append(onion) # Removing an item from the grocery list grocery_list.remove(bread) # Sorting the grocery list alphabetically grocery_list.sort() print("Updated Grocery List:", grocery_list)
copy

The diagram below illustrates the nested list structure of grocery_list. Each item in the list, such as milk, apple, and bread, is itself a list containing a specific item details.

For instance, to access the price of milk, which is stored in the milk sublist, you use the syntax grocery_list[2][1]. Here, grocery_list[2] selects the milk sublist, and grocery_list[2][1] goes a step further to access the second element of that sublist — the price.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 2
toggle bottom row

Nested Lists

A nested list in Python is a list that contains other sublists as its elements. This structure is particularly useful for grouping related items within a main list, where each sublist often shares common attributes or relationships.

To access elements within these sublists, indexing is used sequentially — meaning, we select the main list index first and then the sublist index. The following practical application and diagram provide a detailed look at how you can create and manage nested lists effectively.

Example Application

A customer in your grocery store has compiled a list of grocery items, where each item's details are stored in sublists under variable names.

We will help the customer access details from the milk sublist, add a new item, remove an existing item, and sort the entire grocery_list.

Let's start by creating a list first, and go step by step:

12345678910111213141516171819202122232425
# Define individual grocery items as lists containing details bread = ["Bread", 4.80, 3, "Gluten Free"] # Item name, price, quantity, type milk = ["Milk", 5.99, 2, "2% Milk"] # Item name, price, quantity, type apple = ["Apple", 1.27, 12, "Fuji"] # Item name, price, quantity, type # Create the main grocery list that contains these items grocery_list = [bread, apple, milk] print("Grocery List:" , grocery_list) # Accessing and printing specific item details using indexing print("Item:", grocery_list[2][0]) # Accesses "Milk" title print("Price:", grocery_list[2][1]) # Accesses price of a Milk, which is 5.99 print("Quantity:", grocery_list[2][2]) # Accesses quantity of Milk, which is 2 print("Type:", grocery_list[2][3]) # Accesses type of Milk, which is "2% Milk" # Adding a new sublist item to the grocery list onion = ["Onions", 1.30, 10, "Yellow"] grocery_list.append(onion) # Removing an item from the grocery list grocery_list.remove(bread) # Sorting the grocery list alphabetically grocery_list.sort() print("Updated Grocery List:", grocery_list)
copy

The diagram below illustrates the nested list structure of grocery_list. Each item in the list, such as milk, apple, and bread, is itself a list containing a specific item details.

For instance, to access the price of milk, which is stored in the milk sublist, you use the syntax grocery_list[2][1]. Here, grocery_list[2] selects the milk sublist, and grocery_list[2][1] goes a step further to access the second element of that sublist — the price.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

A nested list in Python is a list that contains other sublists as its elements. This structure is particularly useful for grouping related items within a main list, where each sublist often shares common attributes or relationships.

To access elements within these sublists, indexing is used sequentially — meaning, we select the main list index first and then the sublist index. The following practical application and diagram provide a detailed look at how you can create and manage nested lists effectively.

Example Application

A customer in your grocery store has compiled a list of grocery items, where each item's details are stored in sublists under variable names.

We will help the customer access details from the milk sublist, add a new item, remove an existing item, and sort the entire grocery_list.

Let's start by creating a list first, and go step by step:

12345678910111213141516171819202122232425
# Define individual grocery items as lists containing details bread = ["Bread", 4.80, 3, "Gluten Free"] # Item name, price, quantity, type milk = ["Milk", 5.99, 2, "2% Milk"] # Item name, price, quantity, type apple = ["Apple", 1.27, 12, "Fuji"] # Item name, price, quantity, type # Create the main grocery list that contains these items grocery_list = [bread, apple, milk] print("Grocery List:" , grocery_list) # Accessing and printing specific item details using indexing print("Item:", grocery_list[2][0]) # Accesses "Milk" title print("Price:", grocery_list[2][1]) # Accesses price of a Milk, which is 5.99 print("Quantity:", grocery_list[2][2]) # Accesses quantity of Milk, which is 2 print("Type:", grocery_list[2][3]) # Accesses type of Milk, which is "2% Milk" # Adding a new sublist item to the grocery list onion = ["Onions", 1.30, 10, "Yellow"] grocery_list.append(onion) # Removing an item from the grocery list grocery_list.remove(bread) # Sorting the grocery list alphabetically grocery_list.sort() print("Updated Grocery List:", grocery_list)
copy

The diagram below illustrates the nested list structure of grocery_list. Each item in the list, such as milk, apple, and bread, is itself a list containing a specific item details.

For instance, to access the price of milk, which is stored in the milk sublist, you use the syntax grocery_list[2][1]. Here, grocery_list[2] selects the milk sublist, and grocery_list[2][1] goes a step further to access the second element of that sublist — the price.

Task

Now it's your turn to update an inventory using lists. This challenge involves adjusting your grocery store's vegetable section. Start with a predefined list of vegetables, remove one, add two new ones, and sort the list alphabetically without introducing duplicates.

  1. Create a variable named vegetables and assign it to the list containing "tomatoes", "potatoes", and "onions".
  2. Remove "onions" from the list.
  3. Add "carrots" to the end of the list if it has not already existed in the list.
  4. Add "cucumbers" to the end of the list if it has not already existed in the list.
  5. Sort the list alphabetically.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 2
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt