Challenge - 5 Problems
Tuple vs List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of comparing tuple and list with same elements
What is the output of this code snippet?
Python
print((1, 2, 3) == [1, 2, 3])
Attempts:
2 left
๐ก Hint
Think about how Python compares different data types.
โ Incorrect
Even though the tuple and list have the same elements, Python's == operator returns False when comparing different sequence types.
โ Predict Output
intermediate2:00remaining
Result of comparing nested tuple and list
What will this code print?
Python
print((1, [2, 3]) == (1, [2, 3]))
Attempts:
2 left
๐ก Hint
Look inside the tuple elements carefully.
โ Incorrect
Both tuples contain the same elements, including the same list inside. So the comparison returns True.
โ Predict Output
advanced2:00remaining
Comparing tuple and list with same elements but different order
What is the output of this code?
Python
print((1, 2, 3) == [3, 2, 1])
Attempts:
2 left
๐ก Hint
Order matters in sequences.
โ Incorrect
The tuple and list have the same elements but in different order, so the comparison returns False.
โ Predict Output
advanced2:00remaining
What happens when comparing tuple and list with mutable elements?
What will this code print?
Python
a = [1, 2] b = (1, 2) print(a == b) a[0] = 3 print(a == b)
Attempts:
2 left
๐ก Hint
Check the values before and after changing the list.
โ Incorrect
Initially, the list and tuple have the same elements, so a == b is False because they are different types. After changing the first element of the list, they still differ, so the comparison remains False.
๐ง Conceptual
expert2:00remaining
Why does comparing tuple and list with same elements return True?
Why does Python return True when comparing a tuple and a list with the same elements?
Attempts:
2 left
๐ก Hint
Think about how Python treats different data types in equality checks.
โ Incorrect
Python's == operator for sequences first checks if lengths are equal, then compares elements pairwise recursively. The specific types (tuple or list) do not affect equality; only contents matter. Thus, True.