Muhiim Ali
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## 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>

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully