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
Recall & Review
beginner
What is a nested list in Python?
A nested list is a list that contains other lists as its elements. It's like a box inside another box, where each inner box can hold items.
Click to reveal answer
beginner
How do you access an element inside a nested list?
You use multiple square brackets. The first bracket selects the inner list, and the second bracket selects the element inside that inner list. For example, list[0][1] accesses the second item of the first inner list.
Click to reveal answer
intermediate
How can you add an element to an inner list inside a nested list?
First, access the inner list using its index, then use the append() method. For example, nested[1].append(5) adds 5 to the second inner list.
Click to reveal answer
beginner
What does this code output?
nested = [[1, 2], [3, 4]]
print(nested[1][0])
It outputs 3 because nested[1] is the second inner list [3, 4], and nested[1][0] is the first element of that list, which is 3.
Click to reveal answer
beginner
Can nested lists have different lengths for inner lists?
Yes! Inner lists can have different numbers of elements. For example, [[1, 2], [3, 4, 5]] is valid.
Click to reveal answer
What is the output of this code?
lst = [[10, 20], [30, 40]]
print(lst[0][1])
A20
B10
C30
D40
✗ Incorrect
lst[0] is [10, 20], and lst[0][1] is the second element, which is 20.
How do you add the number 5 to the second inner list in nested = [[1], [2, 3]]?
Anested[0].append(5)
Bnested.append(5)
Cnested[1][5]
Dnested[1].append(5)
✗ Incorrect
nested[1] accesses the second inner list, and append(5) adds 5 to it.
Which of these is a valid nested list?
A[1, 2, 3, 4]
B[[1, 2], [3, 4, 5]]
C[[1, 2], 3, 4]
D[1, [2, 3], 4]
✗ Incorrect
A nested list must have lists inside it. Options B and D have inner lists.
What does nested[2][0] do if nested = [[1], [2, 3]]?
AReturns 2
BReturns 1
CCauses an error because nested has no third inner list
DReturns 3
✗ Incorrect
nested has only two inner lists at indexes 0 and 1, so index 2 is out of range.
How can you print all elements of a nested list nested = [[1, 2], [3, 4]]?
AUse two loops: for inner in nested: for item in inner: print(item)
BUse one loop: for item in nested: print(item)
CUse nested.print()
DUse nested.flatten()
✗ Incorrect
Two loops are needed: one to go through inner lists, one to go through items inside them.
Explain what a nested list is and how you access elements inside it.
Think of a list inside another list like boxes inside boxes.
You got /3 concepts.
Describe how to add an element to an inner list within a nested list.
First find the right inner list, then add the item.
You got /3 concepts.
Practice
(1/5)
1. What is a nested list in Python?
easy
A. A list with only one element
B. A list that contains other lists as its elements
C. A list that cannot be changed
D. A list that stores only numbers
Solution
Step 1: Understand list elements
A list can hold many types of elements, including other lists.
Step 2: Define nested list
A nested list is a list where some elements are themselves lists.
Final Answer:
A list that contains other lists as its elements -> Option B
Quick Check:
Nested list = list inside list [OK]
Hint: Think of a list inside another list [OK]
Common Mistakes:
Confusing nested list with single-level list
Thinking nested list is immutable
Assuming nested list holds only numbers
2. Which of the following is the correct syntax to create a nested list with two inner lists?
easy
A. [[1, 2], [3, 4]]
B. [1, 2, 3, 4]
C. [1, [2, 3], 4]
D. [[1, 2], 3, 4]
Solution
Step 1: Identify nested list syntax
A nested list has lists inside the main list, each enclosed in brackets.
Step 2: Check options
[[1, 2], [3, 4]] has two inner lists: [1, 2] and [3, 4], correctly nested.
Final Answer:
[[1, 2], [3, 4]] -> Option A
Quick Check:
Two inner lists = double brackets [OK]
Hint: Look for double brackets for nested lists [OK]
Common Mistakes:
Using single brackets only
Mixing elements and lists incorrectly
Missing commas between inner lists
3. What is the output of the following code?
matrix = [[1, 2], [3, 4]]
print(matrix[1][0])
medium
A. 3
B. 1
C. 4
D. 2
Solution
Step 1: Access outer list element
matrix[1] accesses the second inner list: [3, 4].
Step 2: Access inner list element
matrix[1][0] accesses the first element of [3, 4], which is 3.
Final Answer:
3 -> Option A
Quick Check:
matrix[1][0] = 3 [OK]
Hint: First index outer list, second index inner list [OK]
Common Mistakes:
Mixing up indexes (using [0][1] instead)
Forgetting zero-based indexing
Confusing inner and outer list positions
4. Find the error in this code snippet:
data = [[1, 2], [3, 4]]
print(data[2][0])
medium
A. No error, output is 3
B. SyntaxError due to missing brackets
C. TypeError because data is not a list
D. IndexError because data[2] does not exist
Solution
Step 1: Check list length
data has two elements: indexes 0 and 1 only.
Step 2: Accessing invalid index
data[2] tries to access a third element which does not exist, causing IndexError.
Final Answer:
IndexError because data[2] does not exist -> Option D
Quick Check:
Index out of range = IndexError [OK]
Hint: Check list length before accessing index [OK]
Common Mistakes:
Assuming index 2 exists
Confusing syntax error with runtime error
Ignoring zero-based indexing
5. Given a nested list nums = [[1, 2, 3], [4, 5], [6]], which code correctly prints all elements in order?
hard
A. print(nums[0][0], nums[1][1], nums[2][0])
B. for i in range(len(nums)):
print(nums[i])
C. for sublist in nums:
for num in sublist:
print(num)
D. for num in nums:
print(num)
Solution
Step 1: Understand nested iteration
To print all elements, loop over each inner list, then each element inside it.
Step 2: Analyze options
for sublist in nums:
for num in sublist:
print(num) uses two loops: outer for sublist, inner for num, printing each number.
Final Answer:
for sublist in nums:
for num in sublist:
print(num) -> Option C
Quick Check:
Nested loops print all elements [OK]
Hint: Use nested loops to access nested list elements [OK]