Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nested list comprehension
๐ Scenario: You work in a bakery that sells boxes of assorted cookies. Each box contains several types of cookies, and you want to create a simple list showing all cookie types available in all boxes.
๐ฏ Goal: Build a nested list comprehension to flatten a list of cookie boxes into a single list of cookie types.
๐ What You'll Learn
Create a list of lists called cookie_boxes with exact cookie types
Create a variable called all_cookies using nested list comprehension
Print the all_cookies list
๐ก Why This Matters
๐ Real World
Bakeries and stores often have grouped data like boxes or categories. Flattening such data helps in searching or displaying all items easily.
๐ผ Career
Understanding nested list comprehension is useful for data processing jobs, where you often work with nested or grouped data structures.
Progress0 / 4 steps
1
Create the cookie boxes data
Create a list called cookie_boxes with these exact lists inside: ["chocolate chip", "oatmeal"], ["peanut butter", "snickerdoodle"], and ["sugar", "gingersnap"].
Python
Hint
Remember, cookie_boxes is a list containing three lists, each with two cookie names as strings.
2
Prepare the variable for all cookies
Create an empty list called all_cookies to hold all cookie types before using list comprehension.
Python
Hint
Just create a variable named all_cookies and set it to an empty list [].
3
Use nested list comprehension to flatten the list
Use nested list comprehension to fill all_cookies with every cookie from each box in cookie_boxes. Use all_cookies = [cookie for box in cookie_boxes for cookie in box] exactly.
Python
Hint
Remember the order: first loop over each box in cookie_boxes, then over each cookie in that box.
4
Print the list of all cookies
Print the all_cookies list using print(all_cookies).
Python
Hint
Use the print() function to show the list.
Practice
(1/5)
1. What does the following nested list comprehension do? [[x * 2 for x in range(3)] for _ in range(2)]
easy
A. Creates a list of two lists, each with numbers from 0 to 3
B. Creates a flat list of numbers doubled from 0 to 5
C. Creates a list of three lists, each with numbers doubled from 0 to 1
D. Creates a list of two lists, each with numbers doubled from 0 to 2
Solution
Step 1: Understand inner list comprehension
The inner part [x * 2 for x in range(3)] creates a list by doubling numbers 0, 1, 2 resulting in [0, 2, 4].
Step 2: Understand outer list comprehension
The outer loop runs twice (for _ in range(2)), repeating the inner list twice, making [[0, 2, 4], [0, 2, 4]].
Final Answer:
Creates a list of two lists, each with numbers doubled from 0 to 2 -> Option D
Quick Check:
Nested loops create repeated doubled lists [OK]
Hint: Inner loop builds list, outer repeats it [OK]
Common Mistakes:
Confusing outer and inner loops
Thinking it creates a flat list
Miscounting range values
2. Which of the following is the correct syntax for a nested list comprehension that creates a 3x3 matrix of zeros?
[[0 for _ in range(3)] for _ in range(3)] uses two for loops inside list brackets correctly: outer and inner loops create rows and columns.
Step 2: Check other options for syntax errors
[0 for _ in range(3) for _ in range(3)] is flat comprehension, not nested. [[0] * 3] * 3 uses list multiplication but not comprehension. [0] * 3 for _ in range(3) is invalid syntax.
Final Answer:
[[0 for _ in range(3)] for _ in range(3)] -> Option C