Tuples
Learn how Python tuples work, why immutability is useful, how tuple unpacking works, and when to choose a tuple over a list.
Introduction
You have just learned that lists are mutable, ordered collections. Python also has a close cousin of the list called a tuple, which is ordered but immutable — once created, its contents cannot be changed.
This lesson explores how to create tuples, why immutability is actually a useful feature rather than a limitation, and a powerful technique called tuple unpacking that you will use constantly in real Python code.
- How to create tuples, including the tricky single-element tuple.
- Why tuples are immutable and where that matters.
- How to unpack tuple values into variables, including extended unpacking.
- The two built-in tuple methods: count() and index().
- When to choose a tuple instead of a list.
What is a Tuple?
A tuple is an ordered collection of items, similar to a list, but written with parentheses instead of square brackets. Like lists, tuples can hold mixed data types and preserve the order in which items were added.
point = (10, 20)
print(point)
print(type(point))(10, 20)
<class 'tuple'>Creating Tuples
You can create a tuple by wrapping comma-separated values in parentheses. In fact, the comma is what actually defines a tuple — the parentheses mostly just make the intent clear.
coordinates = (4, 7)
colors = ("red", "green", "blue")
empty_tuple = ()
print(coordinates)
print(colors)
print(empty_tuple)(4, 7)
('red', 'green', 'blue')
()The Single-Element Tuple Gotcha
A very common beginner surprise: wrapping a single value in parentheses does NOT create a tuple. Python needs a trailing comma to know you mean a tuple, not just a value inside parentheses used for grouping.
not_a_tuple = (5)
actual_tuple = (5,)
print(type(not_a_tuple))
print(type(actual_tuple))<class 'int'>
<class 'tuple'>To create a single-element tuple, you must include a trailing comma: (1,). Without it, (1) is just the integer 1 inside parentheses.
Immutability and Why It Is Useful
Once created, a tuple cannot be changed — no adding, removing, or reassigning items. Attempting to modify a tuple raises a TypeError, just like trying to modify a string in place.
point = (10, 20)
point[0] = 99Traceback (most recent call last):
...
TypeError: 'tuple' object does not support item assignmentThis might sound limiting, but immutability is actually a valuable feature in several situations.
Dictionary Keys
Tuples can be used as dictionary keys because they are immutable; lists cannot.
Fixed Records
Data that should never change, like a date (year, month, day), is safer as a tuple.
Safety
Passing a tuple to a function guarantees the function cannot accidentally modify your data.
Performance
Tuples are generally slightly faster to create and access than lists.
Tuple Unpacking
Tuple unpacking lets you assign each item in a tuple to its own variable in a single line, which is one of the most commonly used tuple features in real Python code.
point = (4, 7)
x, y = point
print(x)
print(y)4
7This also works directly without needing to store the tuple in a variable first, and is commonly used to swap two variables in one line.
a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)1 2
2 1Extended Unpacking
Using an asterisk before a variable name during unpacking captures multiple leftover items into a list, while the other variables capture single values. This is called extended unpacking.
numbers = [1, 2, 3, 4]
first, *rest = numbers
print(first)
print(rest)1
[2, 3, 4]numbers = [1, 2, 3, 4, 5]
first, *middle, last = numbers
print(first)
print(middle)
print(last)1
[2, 3, 4]
5Unpacking, including extended unpacking with *, works the same way on lists as it does on tuples — the values just need to be an iterable of the right length.
Tuple Methods
Because tuples are immutable, they support only two built-in methods — there is nothing to add, remove, or sort in place.
count(value)
Returns how many times a value appears in the tuple.
index(value)
Returns the position of the first occurrence of a value.
numbers = (1, 3, 3, 5, 3, 7)
print(numbers.count(3))
print(numbers.index(5))3
3Tuple vs List: When to Choose Which
Both tuples and lists store ordered collections of items, but they serve different purposes. Choosing the right one communicates your intent clearly to anyone reading your code.
Choose a Tuple
- The data should never change, like coordinates or a date.
- You need to use the collection as a dictionary key.
- You are returning multiple fixed values from a function.
- You want a small performance benefit for read-only data.
Choose a List
- The collection will grow, shrink, or be reordered.
- You need methods like append, remove, or sort.
- You are storing a variable number of similar items, like usernames.
- The data represents an evolving collection, not a fixed record.
def get_dimensions():
return (1920, 1080)
width, height = get_dimensions()
print(f"{width}x{height}")1920x1080Common Mistakes
- Forgetting the trailing comma when creating a single-element tuple.
- Trying to modify a tuple in place, like point[0] = 99.
- Using more or fewer variable names than items when unpacking, which raises a ValueError.
- Assuming tuples can be sorted in place — use sorted(my_tuple) to get a new sorted list instead.
- Choosing a list out of habit for data that never changes, when a tuple better communicates intent.
Best Practices
- Use tuples for fixed, related values like coordinates, RGB colors, or dates.
- Use tuple unpacking to make code that returns multiple values from a function more readable.
- Reach for extended unpacking (*rest) when you only care about the first or last few items.
- Use tuples as dictionary keys when you need a compound key, since lists cannot be used this way.
- Default to lists for collections that will change size or order over the life of the program.
Frequently Asked Questions
Are tuples faster than lists?
Yes, generally. Because tuples are immutable, Python can optimize their creation and access slightly compared to lists.
Can I convert a tuple to a list and back?
Yes. Use list(my_tuple) to convert a tuple to a list, and tuple(my_list) to convert a list to a tuple.
Why does (5) not create a tuple?
Parentheses alone are just used for grouping in Python. A tuple requires a comma, so you need (5,) to create a genuine single-element tuple.
Can a tuple contain a mutable object like a list?
Yes. The tuple itself cannot be changed (you cannot replace its items), but if one of its items is a mutable list, that inner list can still be modified.
What happens if I unpack a tuple into the wrong number of variables?
Python raises a ValueError, such as "too many values to unpack", unless you use extended unpacking with an asterisk to absorb the extra values.
Key Takeaways
- Tuples are ordered, immutable collections created with parentheses and commas.
- A single-element tuple needs a trailing comma, like (5,).
- Immutability makes tuples safe for fixed records and usable as dictionary keys.
- Tuple unpacking assigns each item to its own variable in one line.
- Extended unpacking with * captures multiple leftover items into a list.
- Tuples support only count() and index(), since their contents cannot change.
- Choose tuples for fixed data and lists for collections that change over time.
Summary
Tuples give you an ordered, immutable way to group related values together, which makes your code safer and your intent clearer whenever data should not change. Tuple unpacking is a technique you will use throughout your Python journey, from simple assignments to function return values.
Next, you will learn about sets — an unordered collection type built for storing unique values and performing fast membership checks.
- You can create tuples and avoid the single-element tuple gotcha.
- You understand why immutability is a useful feature, not just a restriction.
- You can unpack tuples, including with extended unpacking.
- You know when to choose a tuple over a list.
- You are ready to learn about sets.