Challenge - 5 Problems
Tuple Unpacking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested tuple unpacking
What is the output of this Python code?
Python
data = (1, (2, 3), 4) a, (b, c), d = data print(a, b, c, d)
Attempts:
2 left
๐ก Hint
Remember that nested tuples can be unpacked by matching the structure.
โ Incorrect
The tuple data has three elements: 1, (2, 3), and 4. The unpacking assigns 1 to a, (2, 3) to (b, c), and 4 to d. Then b and c get 2 and 3 respectively.
โ Predict Output
intermediate2:00remaining
Unpacking with star expression
What will be printed by this code?
Python
t = (10, 20, 30, 40, 50) a, *b, c = t print(a, b, c)
Attempts:
2 left
๐ก Hint
The star expression collects all middle elements as a list.
โ Incorrect
The first element 10 is assigned to a, the last element 50 to c, and the middle elements [20, 30, 40] are collected into list b.
๐ง Conceptual
advanced2:00remaining
Error type from wrong unpacking
What error does this code raise when run?
Python
x, y = (1, 2, 3)
Attempts:
2 left
๐ก Hint
Check how many values are on the right vs variables on the left.
โ Incorrect
The tuple has 3 values but only 2 variables to unpack into, so Python raises a ValueError about too many values.
โ Predict Output
advanced2:00remaining
Unpacking with nested star expressions
What is the output of this code?
Python
a, *b, (c, d) = (1, 2, 3, (4, 5)) print(a, b, c, d)
Attempts:
2 left
๐ก Hint
The star expression collects all elements except first and last, last is a tuple unpacked into c and d.
โ Incorrect
a gets 1, b gets [2, 3], and (c, d) unpacks the last tuple (4, 5) into c=4 and d=5.
โ Predict Output
expert3:00remaining
Complex unpacking with multiple levels
What will be the output of this code?
Python
data = (1, (2, (3, 4)), 5) (x, (y, (z, w)), v) = data print(x, y, z, w, v)
Attempts:
2 left
๐ก Hint
Match each variable to the corresponding nested tuple element.
โ Incorrect
The tuple data has 3 elements: 1, (2, (3, 4)), and 5. The unpacking assigns 1 to x, 2 to y, 3 to z, 4 to w, and 5 to v.