Course Content
Introduction to Python (copy)
Introduction to Python (copy)
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:
# 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)
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.
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
Thanks for your feedback!
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:
# 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)
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.
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
Thanks for your feedback!