0
0
Pythonprogramming~20 mins

Iterating over lists in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of List Iteration
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested loops with list iteration
What is the output of this Python code?
Python
result = []
for i in [1, 2]:
    for j in [3, 4]:
        result.append(i * j)
print(result)
A[3, 4, 6, 8]
B[3, 6, 4, 8]
C[4, 3, 8, 6]
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Think about how the outer and inner loops multiply each pair of numbers.
Predict Output
intermediate
2:00remaining
Output of list comprehension with condition
What is the output of this code?
Python
numbers = [0, 1, 2, 3, 4]
squares = [x**2 for x in numbers if x % 2 == 0]
print(squares)
A[0, 1, 4, 9, 16]
B[1, 9]
C[0, 4, 16]
D[2, 4]
Attempts:
2 left
💡 Hint
Only even numbers are squared and included.
🔧 Debug
advanced
2:00remaining
Identify the error in list iteration
What error does this code raise when run?
Python
items = [1, 2, 3]
for i in range(len(items)):
    print(items[i+1])
AIndexError
BTypeError
CSyntaxError
DNo error, prints 2, 3, and 4
Attempts:
2 left
💡 Hint
Check the index used inside the loop compared to the list length.
Predict Output
advanced
2:00remaining
Output of modifying list during iteration
What is the output of this code?
Python
lst = [1, 2, 3]
for x in lst:
    lst.append(x + 3)
    if len(lst) > 6:
        break
print(lst)
A[1, 2, 3, 4, 5, 6, 7]
B[1, 2, 3]
CInfinite loop
D[1, 2, 3, 4, 5, 6]
Attempts:
2 left
💡 Hint
Appending to a list during 'for x in lst' iteration only processes the original elements; the break checks length after each append.
🧠 Conceptual
expert
2:00remaining
Number of items after complex iteration
How many items are in the list 'result' after running this code?
Python
result = []
for i in range(3):
    for j in range(i):
        result.append(i + j)
print(len(result))
A4
B3
C6
D0
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i.