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
Tuple vs List Comparison
๐ Scenario: Imagine you are organizing two types of collections: one that should never change, like the days of the week, and one that can change, like a shopping list.
๐ฏ Goal: You will create a tuple and a list with the same items, then compare them to see how Python treats these two types differently.
๐ What You'll Learn
Create a tuple called week_days with the days: 'Monday', 'Tuesday', 'Wednesday'
Create a list called shopping_list with the items: 'Monday', 'Tuesday', 'Wednesday'
Create a variable called are_equal that compares week_days and shopping_list using the equality operator
Print the value of are_equal
๐ก Why This Matters
๐ Real World
Tuples are used when you want to store data that should not change, like fixed settings or days of the week. Lists are used when you need to add or remove items, like a shopping list.
๐ผ Career
Understanding the difference between tuples and lists helps in writing clear and efficient code, which is important in many programming jobs.
Progress0 / 4 steps
1
Create the tuple week_days
Create a tuple called week_days with these exact values: 'Monday', 'Tuesday', 'Wednesday'
Python
Hint
Use parentheses ( ) to create a tuple.
2
Create the list shopping_list
Create a list called shopping_list with these exact values: 'Monday', 'Tuesday', 'Wednesday'
Python
Hint
Use square brackets [ ] to create a list.
3
Compare week_days and shopping_list
Create a variable called are_equal that stores the result of comparing week_days and shopping_list using the == operator
Python
Hint
Use the equality operator == to compare the tuple and list.
4
Print the comparison result
Print the value of the variable are_equal
Python
Hint
Use print(are_equal) to show the result.
Practice
(1/5)
1. Which of the following statements about tuples and lists in Python is correct?
easy
A. Both tuples and lists use parentheses and are mutable.
B. Tuples use square brackets and are mutable; lists use parentheses and are immutable.
C. Tuples use parentheses and are immutable; lists use square brackets and are mutable.
D. Both tuples and lists use square brackets and are immutable.
Solution
Step 1: Identify tuple and list syntax
Tuples are created with parentheses (), and lists with square brackets [].
Step 2: Understand mutability
Tuples cannot be changed after creation (immutable), lists can be changed (mutable).
Final Answer:
Tuples use parentheses and are immutable; lists use square brackets and are mutable. -> Option C
Quick Check:
Tuple = immutable, parentheses; List = mutable, brackets [OK]
2. Which of the following is the correct way to compare a tuple (1, 2, 3) with a list [1, 2, 3] for equality in Python?
easy
A. (1, 2, 3) is [1, 2, 3]
B. (1, 2, 3) == [1, 2, 3]
C. list((1, 2, 3)) == (1, 2, 3)
D. tuple([1, 2, 3]) == (1, 2, 3)
Solution
Step 1: Understand direct comparison
Comparing a tuple and list directly returns False because they are different types.
Step 2: Convert list to tuple for comparison
Converting the list to a tuple makes the types match, so equality checks content and order.
Final Answer:
tuple([1, 2, 3]) == (1, 2, 3) -> Option D
Quick Check:
Convert list to tuple before comparing [OK]
Hint: Convert list to tuple before comparing to tuple [OK]
Common Mistakes:
Comparing tuple and list directly
Using 'is' instead of '=='
Converting tuple to list instead of list to tuple
3. What is the output of the following code?
t = (4, 5, 6)
l = [4, 5, 6]
print(t == tuple(l))
medium
A. True
B. False
C. TypeError
D. None
Solution
Step 1: Convert list to tuple
tuple(l) converts list [4, 5, 6] to tuple (4, 5, 6).
Step 2: Compare tuples
t == tuple(l) compares (4, 5, 6) == (4, 5, 6), which is True.
Final Answer:
True -> Option A
Quick Check:
Same content and type means equality True [OK]
Hint: Convert list to tuple to compare with tuple [OK]
Common Mistakes:
Expecting False because types differ
Confusing '==' with 'is'
Forgetting to convert list before comparing
4. The following code tries to check if a tuple and a list have the same elements:
t = (7, 8, 9)
l = [7, 8, 9]
if t == l:
print("Equal")
else:
print("Not equal")
What is the problem and how to fix it?
medium
A. No problem; it prints "Equal".
B. It prints "Not equal" because tuple and list types differ; fix by converting list to tuple.
C. It causes a TypeError; fix by converting tuple to list.
D. It prints "Not equal" because elements differ; fix by sorting both.
Solution
Step 1: Understand type difference in comparison
Comparing tuple and list directly returns False even if elements match.
Step 2: Fix by converting list to tuple
Convert list to tuple before comparing to match types and check content equality.
Final Answer:
It prints "Not equal" because tuple and list types differ; fix by converting list to tuple. -> Option B
Quick Check:
Convert list to tuple for correct equality check [OK]
Hint: Convert list to tuple before comparing to tuple [OK]
Common Mistakes:
Assuming direct comparison works
Trying to convert tuple to list instead
Ignoring type difference in equality
5. You have a list of mixed tuples and lists:
data = [(1, 2), [1, 2], (3, 4), [3, 4]]
You want to count how many unique pairs are present, considering tuples and lists with the same elements as equal. Which code correctly counts unique pairs?
hard
A. unique = set(tuple(x) for x in data)
print(len(unique))
B. unique = set(list(x) for x in data)
print(len(unique))
C. unique = set(data)
print(len(unique))
D. unique = set(data)
print(len(unique))
# Then convert to list
Solution
Step 1: Understand the problem
Tuples and lists with same elements should be treated as equal pairs.
Step 2: Convert all elements to tuples
Convert each item to tuple so all pairs have the same type for comparison and hashing in set.
Step 3: Use set to find unique pairs
Set removes duplicates, so length of set is count of unique pairs.
Final Answer:
unique = set(tuple(x) for x in data)
print(len(unique)) -> Option A
Quick Check:
Convert all to tuple before set to count unique pairs [OK]
Hint: Convert all to tuple before set to count unique pairs [OK]