What you will learn
- Define and identify: nested list
- Use nested for loops to traverse through nested lists
Do Now
In your Console
Type and run the following code
# my_building is a representation of the apartments on each floor of my 3 story building
my_building = [
['apt1a', 'apt1b', 'apt1c'],
['apt2a', 'apt2b', 'apt2c'],
['apt3a', 'apt3b', 'apt3c']
]
print("first floor: " + str(my_building[0]))
print("first floor, 3rd apartment: " + str(my_building[0][2]))
In your Notebook
Respond to the following
- Write down what was printed.
- How you would access the 2nd apartment of the 3rd floor (
apt3b)?
Lab
Part 1
The goal of this lab is to practice using and accessing items from lists of lists.
You have a few errands to run and have created a few shopping list to help you remember what to buy. You stored your notes in a nested list, shopping_cart.
This program will allow the user to ask for a specific item by it’s index or update what items are in the cart. The user can request to view list to see the items in a specific shopping list.
Shopping Cart
shopping_cart = [
['tooth paste', 'q-tips', 'milk'],
['milk', 'candy', 'apples'],
['planner', 'pencils', 'q-tips']
]
User Inputs
update
- The program asks which shopping list the user wants to update, which position it should update, and the new value to update.
view item
- The program asks which shopping list the item is on and which position i occupies, then prints the items name.
view list
- The program asks which shopping list the user wants and prints all of the items associated with that shopping list.
Functions
update_list
- Takes in an integer representing the index of the shopping list, an integer representing the index of the item to update, and a string representing the new value for that item. Does not alter the length of the list.
print_item
- Takes an int representing the index of the shopping list followed by an int representing the index of the item to print.
print_list
- Takes an int representing the index of the shopping list to print.
- Feel free to add more functions as you see fit
Example
>>>What would you like to do? view list
Which shopping list would you like to see? 1
tooth paste, q-tips, gum
Part 2
In this part of the lab you will go through your shopping list program and perform a few different calculations.
- Create a function,
all_in_one, that will put all the shopping lists into a single list using a for loop. - Create a function,
count_q_tips, which will go through all items of the list and keep a count of how many times'q-tips'occurs. - In order to make the shopping lists more calcium rich, write a function,
drink_more_milk, that adds'milk'to each of the lists (unless it’s already there). - You can’t have milk without cookies. Write a function
if_you_give_a_moose_a_cookie, that will go through every element ofshopping_cartand update'milk'to be'milk and cookies'.
Bonus
Write a function to reverse the order of the lists and items in shopping_cart.
The list should look like the following when printed:
shopping_cart = [
['q-tips', 'pencils', 'planner'],
['apples', 'candy', 'milk'],
['milk', 'q-tips', 'tooth paste']
]
Tip
- Last item can be gotten by
my_list[-1] - Second to last element:
my_list[-2] - Third to last element:
my_list[-3]
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What is a nested list?
- A list that is empty
- A list whose items are themselves lists
- A list sorted in order
- A list that cannot change
Nested lists are lists that contain other lists as items.
2. Given grid = [[1, 2], [3, 4], [5, 6]], what is grid[2][0]?
- 3
- 2
- 5
- 6
grid[2] is [5, 6], and index 0 of that is 5.
3. How do you visit every item in a nested list?
- Use one loop and
lenonly - Loop over the outer list, and inside it loop over each inner list
- Use
range(0) - Print the list once
Nested loops handle the two levels: one for the outer list, one for each inner list.