Challenge - 5 Problems
List Comprehension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of simple list comprehension
What is the output of this Python code?
Python
result = [x * 2 for x in range(4)] print(result)
Attempts:
2 left
๐ก Hint
Think about what range(4) produces and how each element is multiplied by 2.
โ Incorrect
range(4) produces 0,1,2,3. Multiplying each by 2 gives 0,2,4,6.
โ Predict Output
intermediateList comprehension with condition
What is the output of this code?
Python
result = [x for x in range(6) if x % 2 == 0] print(result)
Attempts:
2 left
๐ก Hint
The condition filters numbers divisible by 2.
โ Incorrect
The list comprehension includes only even numbers from 0 to 5, which are 0, 2, and 4.
โ Predict Output
advancedNested list comprehension output
What is the output of this nested list comprehension?
Python
result = [(x, y) for x in range(2) for y in range(3)] print(result)
Attempts:
2 left
๐ก Hint
The first loop is x, the second is y, so y changes faster.
โ Incorrect
The comprehension pairs each x with every y in order, producing all combinations with x from 0 to 1 and y from 0 to 2.
โ Predict Output
advancedList comprehension with else clause (ternary)
What is the output of this code?
Python
result = [x if x % 2 == 0 else -x for x in range(5)] print(result)
Attempts:
2 left
๐ก Hint
If x is even, keep it; if odd, make it negative.
โ Incorrect
For each number from 0 to 4, even numbers stay the same, odd numbers become negative.
๐ง Conceptual
expertUnderstanding list comprehension order
Which option correctly describes the order in which elements are generated in this list comprehension?
result = [(x, y) for x in range(3) for y in range(2)]
Attempts:
2 left
๐ก Hint
Think about how nested loops work in Python.
โ Incorrect
The comprehension is like nested loops: for each x, it loops through all y values, pairing them in order.
