## Remind them how nested list work First, before we walk through nested loops exercises, we want you to check out these resources that might help you or put the concept into a different perspective. Check them out below: - [Beginner Python Tutorial 76 - Nested for Loops](https://www.youtube.com/watch?v=O60CFmcWyEY) (video) - [Nested loops in Python are easy ➿](https://www.youtube.com/watch?v=APWy6Pc83gE) (video) - [2D Lists & Nested Loops - Python - Tutorial 24](youtube.com/watch?v=B5GhlXhDfoE) (video) - [Geeks for Geeks - Python Nested Loops](https://www.geeksforgeeks.org/python-nested-loops/#) Remeber: a nested loop is simply having one (or more) loop(s) inside of another. The inner loop runs completely for each iteration of the outer loop. For example, suppose our outer loop was 10 years. Our inner loop would be 365 days! For each one year, we go through 365 days, repeating this 10 times until the 10th year has been completed. We can even represent this with code: ```python= for i in range(1,11): #our outer loop, the years that encapsulate each day for j in range (1,366): #our inner loop, the days that go from 1-365, repeating at a new iteration of a new year print("day: ", j, " of year: " ,i) #The output will be really long! It looks like this: day j of year i ``` ## Exercises: Use this [Colab link](https://colab.research.google.com/drive/1NrQQhLHZEqc3xl0otRKeBgfjTMqNWPfr?usp=sharing) and make a copy. ### Task 1: Create a nested list to represent the following information about fruits and their colors: Fruits with colors: - Red: Apple, Cherry - Yellow: Banana - Orange: Orange - Green: Kiwi, Grapes - Purple: Plum When we set up our nested list, it's important to think about how we want to organize our information, especially considering that this will guide our understanding of how to set up the loop. So, keeping in mind the way the information is currently presented, what lists do we want to nest within our large fruit list? How will we organize and categorize items in the list? Think about these questions, try creating your list, then compare your solution with the one below. <details> <summary><b>Think, then click!</b></summary> It looks like we've already categorized our fruits based on colors. Therefore, within our large list of fruits, our sublists should be organized by colors. ```python= fruits = [ ["apple","cherry"], #red ["banana"], #yellow ["orange"], #orange ["kiwi", "grape"] #green ["plum"] #purple ] ``` </details> Note: in our example of student grades, each student had the same amount of grades. Here, you can see that each list has a different amount of fruits, which is fine! Having the same amount of values in each list helps to visualize the sometimes grid-like structure of nested lists, but it's not necessary in python. ### Task 2: Try to access and print the following elements from the nested list: Let's review how to access elements of a nested list. We often take the `list[row][col]` appraoch, meaning we index into a specific column belonging to a specific row. You can also think of this in other ways, such as accessing a certain value belonging to a larger container of data (that's then stored in one big overarching container). Here is a table showing the index of each color category in the list: | Color | Index | Fruits | | -------- | ----- | ------------------------ | | Red | 0 | Apple, Cherry | | Yellow | 1 | Banana | | Orange | 2 | Orange | | Green | 3 | Kiwi, Grapes | | Purple | 4 | Plum | Let's practice this! Remember, when using the `list[row][col]` approach, the row corresponds to the index of the color category, and the column corresponds to the specific piece of data within that color category, in this case, fruits. To check your understanding, try accessing the following fruits: 1. The first fruit in the Red category <details> <summary><b>Think, then click!</b></summary> The red category is the first row, of our larger list of rows, so we want to index into row 0. Since we want the first fruit within this category, we want to index into column 0 within this row. ```python= fruits[0][0] ``` </details> <br> 2. The second fruit in the Green category <details> <summary><b>Think, then click!</b></summary> ```python= fruits[3][1] ``` </details> <br> 3. The only fruit in the Yellow category <details><summary><b>Think, then click!</b></summary> ```python= fruits[1][0] ``` </details> <br> 4. The last fruit in the Purple category <details><summary><b>Think, then click!</b></summary> ```python= fruits[4][-1] ``` Since there's only one fruit here `fruits[4][0]` would also work, but dont't forget that using negative indexes allow us to start from the end and go backwards! Anytime we want to easily access the last element of a list (and possibly don't know its length), we can just use -1. </details> <br> **Bonus Question:** what would happen if we tried to access a second green fruit? Or, a third red fruit? An IndexOutOfBounds error! ### Task 3: Add the following fruits to their respective color categories When we index into our nested lists, we don't *have* to index into the row *and* column. Considering that we want to add new fruits (columns) to their respective colors (rows), we only need to index into the row, then add our fruits into that row. Since this requires accessing one row, which is its own list, do you recall what function we can use to add to the list? That's right, it's `append()`! In summary, we want to index into the row only, then append our fruits to that row. What if we wanted to add a completely new row? We'd still use `append()`, right? But do we need to use any fancy indexing schemes? <details><summary><b>Think, then click!</b></summary> Nope! It's just like how we append new elements to one-dimensional arrays. You can think of the list containing all the fruits as one dimension, and the actual fruits themselves as the second dimension. Therefore, you can simply call the function on your entire fruits list, inputting your new list as the argument. Check out the examples below for more clarity. </details> <br> Try each exercise for yourself, then check your work. - Blue: Blueberry <details><summary><b>Think, then click!</b></summary> Since there's no blue row yet, we have to append a new list containing "blueberry" to the fruits list. ```python= fruits.append(["blueberry"]) ``` </details> <br> - Red: Strawberry <details><summary><b>Think, then click!</b></summary> ```python= fruits[0].append("strawberry") ``` </details> <br> - Yellow: Lemon <details><summary><b>Think, then click!</b></summary> ```python= fruits[1].append("lemon") ``` </details> <br> - Green: Pear <details><summary><b>Think, then click!</b></summary> ```python= fruits[3].append("pear")) ``` </details> <br> - Purple: Grape <details><summary><b>Think, then click!</b></summary> ```python= fruits[4].append("grape")) ``` </details> <br> ### Task 4: Use nested loop to remove the fruits that have seeds from their color categories: Here are the fruits with seeds: - Seeds: Kiwi, Strawberry, Banana And this how our fruits list look like now: ```python = fruits = [ ['apple', 'cherry', 'strawberry'], ['banana', 'lemon'], ['orange'], ['kiwi', 'grape', 'pear'], ['plum', 'grape'], ['blueberry'] ] ``` Before you even start coding, it is important to think about how you are going to approach the problem. Here are a few guiding questions: - What do we want the outer loop to iterate over? - What about the inner loop? - How do we check if a fruit has seeds? - What do we want our output to look like? A nested list? A single list? There are so many different ways to solve this problem, but here is one approach: <details><summary><b>Think, then click!</b></summary> - We want the outer loop to go through each category of fruits - We want the the inner loop to go through each individual fruit in that category. ```python = for category in fruits: # Iterate through each fruit in the category for fruit in category: ``` - It is a good idea to create a list that contains the fruits with seeds. So, to check if a fruit has seeds or not, you can check if it is in this list. ```python= seeds = ['Kiwi', 'Strawberry', 'Banana'] ``` - We want our output to be a nested list. So we first need to create an empty list to store the updated fruit categories. Then initialize an empty list to store the non-seed fruits for each category. This is how our code will look: ```python = # Create an empty list to store the updated fruit categories updated_fruits = [] # Iterate through each category of fruits for category in fruits: # Create an empty list to store the non-seed fruits for this category non_seed_fruits = [] # Iterate through each fruit in the category for fruit in category: # Check if the fruit is not in the seeds list if fruit.lower() not in seeds: # If the fruit is not in the seeds list, add it to the non_seed_fruits list non_seed_fruits.append(fruit) # Add the non-seed fruits to the updated_fruits list updated_fruits.append(non_seed_fruits) print(updated_fruits) ``` </details> <br> ### Task 5: Write a function that takes a nested list of numbers as input and returns the sum of all elements. Hint: use nested loops For the input nested list: ```python = nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ``` The output should be: ```python = 45 ``` Again think about what steps you need to take to solve this problem. - Think about the input and output our function. - What data structure, if needed, do you want to use to store the updated data? - Think about what you want the outer loop and inner loop to iterate over. - What do you need to keep track of the sum of the numbers? <details><summary><b>Think, then click!</b></summary> - When creating a function, we use the keyword 'def', followed by the function name, and end it with parentheses. Inside the parentheses, we pass in a parameter because we know our function will be taking in a nested list. This parameter acts as a placeholder for the actual data that will be passed into the function when it is called. - Since we're only returning the sum of the numbers, we don't need to create any kind of data structure to hold our output. However, we do need to create a counter, which is a variable of type int, to hold the total sum of the numbers, and we just return that. - We want the outer loop to iterate over the sublists and we want the inner loop to go through the numbers in each sublist. ```python = def sum_nested_list(nested_list): total_sum = 0 for sublist in nested_list: for num in sublist: total_sum += num return total_sum nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Calling our function: result = sum_nested_list(nested_list) print(result) # Output: 45 ``` </details> <br>