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

Course Content

Introduction to Python (copy)

Introduction to Python (copy)

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

book
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

Swipe to start coding

Update an inventory list for a grocery store's vegetable section by removing an item, adding two new items, and sorting the list alphabetically without duplicates.

  • Create a variable vegetables with the list ["tomatoes", "potatoes", "onions"].
  • Remove "onions" from the list.
  • Add "carrots" to the list if it’s not already there.
  • Add "cucumbers" to the list if it’s not already there.
  • Sort the list alphabetically.

Output Requirements

  • Print the updated vegetable list: "Updated Vegetable Inventory: <$vegetables>".
  • If "carrots" is already in the list, print: "Carrots are already in the list."
  • If "cucumbers" is already in the list, print: "Cucumbers are already in the list."

Solution

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 4. Chapter 2
toggle bottom row

book
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

Swipe to start coding

Update an inventory list for a grocery store's vegetable section by removing an item, adding two new items, and sorting the list alphabetically without duplicates.

  • Create a variable vegetables with the list ["tomatoes", "potatoes", "onions"].
  • Remove "onions" from the list.
  • Add "carrots" to the list if it’s not already there.
  • Add "cucumbers" to the list if it’s not already there.
  • Sort the list alphabetically.

Output Requirements

  • Print the updated vegetable list: "Updated Vegetable Inventory: <$vegetables>".
  • If "carrots" is already in the list, print: "Carrots are already in the list."
  • If "cucumbers" is already in the list, print: "Cucumbers are already in the list."

Solution

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 4. Chapter 2
Switch to desktopSwitch 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