Tuples & Type Conversion
A tuple is an immutable ordered sequence — like a list that cannot be changed after creation. Tuples are used for fixed collections of values and are essential for multiple return values, dictionary keys, and data integrity.
Learning Objectives
- 11.2.4.1 Use tuples
- 11.2.4.2 Convert types between tuples, lists, and sets
Conceptual Anchor
The Sealed Envelope Analogy
A tuple is like a sealed envelope — once you put items inside and seal it, you can read the contents (indexing) but cannot add, remove, or change anything (immutable). A list is an open folder — you can freely modify its contents. Use tuples when data should remain constant.
Rules & Theory
Creating Tuples
# Method 1: Parentheses
point = (3, 5)
rgb = (255, 128, 0)
name_age = ("Ali", 16)
# Method 2: Without parentheses (packing)
coords = 10, 20, 30 # Same as (10, 20, 30)
# Method 3: tuple() constructor
t = tuple([1, 2, 3]) # From list
t2 = tuple("hello") # ('h', 'e', 'l', 'l', 'o')
# Single-element tuple — MUST have trailing comma!
single = (5,) # ✓ Tuple with one element
not_tuple = (5) # ✗ Just the integer 5!
# Empty tuple
empty = ()
also_empty = tuple()Tuple vs List
| Feature | Tuple | List |
|---|---|---|
| Syntax | (1, 2, 3) |
[1, 2, 3] |
| Mutable? | No (immutable) | Yes (mutable) |
| Use for | Fixed data, dict keys, function returns | Dynamic collections |
| Speed | Faster | Slower |
| Can be dict key? | Yes | No |
| Can be set element? | Yes | No |
Tuple Operations
t = (10, 20, 30, 40, 50)
# Indexing and slicing (same as lists)
print(t[0]) # 10
print(t[-1]) # 50
print(t[1:4]) # (20, 30, 40)
# Cannot modify!
# t[0] = 99 # TypeError: 'tuple' object does not support item assignment
# Methods (only 2)
print(t.count(20)) # 1
print(t.index(30)) # 2
# Concatenation and repetition
t2 = t + (60, 70) # (10, 20, 30, 40, 50, 60, 70)
t3 = (1, 2) * 3 # (1, 2, 1, 2, 1, 2)
# Unpacking
x, y, z = (10, 20, 30)
print(x, y, z) # 10 20 30
# Swap using tuple unpacking
a, b = 5, 10
a, b = b, a # a=10, b=5Type Conversion
# List → Tuple
lst = [1, 2, 3]
t = tuple(lst) # (1, 2, 3)
# Tuple → List
t = (4, 5, 6)
lst = list(t) # [4, 5, 6]
# List → Set (removes duplicates)
lst = [1, 2, 2, 3, 3, 3]
s = set(lst) # {1, 2, 3}
# Set → List (order may vary)
lst2 = list(s) # [1, 2, 3]
# Set → Tuple
t2 = tuple(s) # (1, 2, 3)
# String → List/Tuple
list("abc") # ['a', 'b', 'c']
tuple("abc") # ('a', 'b', 'c')
# Conversion chain: remove duplicates + sort
data = [5, 3, 1, 3, 5, 2]
clean = sorted(set(data)) # [1, 2, 3, 5]When to Use Each Type
Tuple: coordinates, RGB colors, function return values, dict keys. List: shopping list, student grades, any collection that changes. Set: unique values, membership testing, eliminating duplicates.
Worked Examples
1 Function Returning Multiple Values
def min_max(numbers):
return min(numbers), max(numbers) # Returns a tuple
result = min_max([4, 7, 1, 9, 3])
print(result) # (1, 9)
print(type(result)) # <class 'tuple'>
# Unpacking the return value
lo, hi = min_max([4, 7, 1, 9, 3])
print(f"Min: {lo}, Max: {hi}") # Min: 1, Max: 92 Converting Between Types
# Remove duplicates from a list while keeping order
original = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
# Method 1: set → list (loses order)
unique_unordered = list(set(original))
print(unique_unordered) # Order may vary
# Method 2: sorted set (sorted order)
unique_sorted = sorted(set(original))
print(unique_sorted) # [1, 2, 3, 4, 5, 6, 9]
# Method 3: keep original order (using dict.fromkeys)
unique_ordered = list(dict.fromkeys(original))
print(unique_ordered) # [3, 1, 4, 5, 9, 2, 6]3 Tuple as Dictionary Key
# Tuples can be dictionary keys (lists cannot!)
distances = {
("Almaty", "Astana"): 1200,
("Almaty", "Shymkent"): 700,
("Astana", "Karaganda"): 230
}
route = ("Almaty", "Astana")
print(f"Distance: {distances[route]} km")Pitfalls & Common Errors
Single-Element Tuple
(5) is just the integer 5. For a single-element tuple, use
(5,) with a trailing comma!
Trying to Modify a Tuple
t[0] = 99 raises TypeError. To "change" a tuple: convert to list →
modify → convert back: lst = list(t); lst[0] = 99; t = tuple(lst).
set() Loses Order
Converting to set and back loses the original order. If order matters, use
dict.fromkeys() or a manual loop.
Pro-Tips for Exams
Tuple Tips
- Tuples have only 2 methods:
.count()and.index() - Tuple unpacking:
a, b, c = (1, 2, 3)— number of variables must match - Swap trick:
a, b = b, auses tuple packing/unpacking - Common conversion question: list → set → sorted list (removes duplicates + sorts)
Graded Tasks
What are the 3 key differences between tuples and lists?
Why is (5) not a tuple but (5,) is? What is the type of each?
Write a function that takes a list of numbers and returns a tuple of (minimum, maximum, average).
Given a list with duplicates, convert it to a sorted tuple of unique values.
What is the output?
a = [1,2,3]; b = tuple(a); c = list(b); c.append(4); print(a, b, c)
Create a program that stores student records as tuples (name, grade, score) in a list, and allows sorting by any field.
Self-Check Quiz
x, y = (10, 20) do?