Creating and Using Tuples
A tuple is an ordered, immutable collection of items. Tuples are defined using parentheses () and look similar to lists but cannot be changed after creation.
Because they are immutable, tuples are slightly faster than lists and can be used as dictionary keys or set members, which lists cannot.
Example
# Creating tuples
coordinates = (10, 20)
colors = ("red", "green", "blue")
single = (42,) # Note the comma — needed for single-item tuple
empty = ()
# Accessing elements (same as lists)
print(colors[0]) # red
print(colors[-1]) # blue
print(colors[1:3]) # ('green', 'blue')
# Tuples are immutable
# colors[0] = "yellow" # TypeError: 'tuple' does not support item assignment
# Tuple length and membership
print(len(colors)) # 3
print("red" in colors) # True
print(colors.count("red")) # 1
print(colors.index("blue"))# 2 - Tuples are defined with parentheses: (item1, item2, item3)
- A single-item tuple needs a trailing comma: (42,) not (42)
- Tuples support indexing, slicing, and iteration like lists
- Tuples have only two methods: .count() and .index()
- Tuples are hashable and can be used as dictionary keys
Try Tuples
JavaScript
# Create a tuple
colors = ("red", "green", "blue")
print("Colors:", colors)
print("First:", colors[0])
print("Last:", colors[-1])
print("Length:", len(colors))
print("Has red?", "red" in colors) Notes
- A common mistake is writing (42) thinking it's a tuple — it's just the integer 42 in parentheses. You need the comma: (42,).
Tuple Packing and Unpacking
Tuple packing is when you assign multiple values to a single variable as a tuple. Tuple unpacking is the reverse — assigning each value in a tuple to separate variables.
Unpacking is incredibly useful and is one of Python's most elegant features. It works with any iterable, not just tuples.
Example
# Tuple packing
person = "Alice", 30, "Engineer" # Parentheses optional when packing
print(person) # ('Alice', 30, 'Engineer')
# Tuple unpacking
name, age, job = person
print(name) # Alice
print(age) # 30
print(job) # Engineer
# Swap variables without a temp variable
a = 10
b = 20
a, b = b, a # Elegant swap using tuple unpacking
print(a, b) # 20, 10
# Extended unpacking with *
numbers = (1, 2, 3, 4, 5)
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
# Returning multiple values from a function
def get_user():
return "Alice", 30 # Returns a tuple
name, age = get_user()
print(f"{name} is {age}") Try Tuple Unpacking
JavaScript
# Packing and unpacking
person = ("Alice", 30, "Engineer")
name, age, job = person
print(f"{name}, age {age}, works as {job}")
# Swap trick
a, b = 10, 20
print(f"Before: a={a}, b={b}")
a, b = b, a
print(f"After: a={a}, b={b}")
# Extended unpacking
first, *rest = [1, 2, 3, 4, 5]
print(f"First: {first}, Rest: {rest}") Notes
- Use tuples when you have a fixed collection of related values (like coordinates, RGB colors, or database rows) that shouldn't change. Use lists when the collection may need to grow or shrink.
