Learning Guides
Tuples in Python: Complete Guide with Examples
Quick answer: Learn Python tuples including creation, indexing, immutability, tuple unpacking, and the key differences between tuples and lists.
What is a Tuple?
A tuple is an ordered, immutable collection of items. Once created, its contents cannot be changed, which makes tuples useful for data that should stay fixed, such as coordinates or a database record.
point = (4, 5)
colors = ('red', 'green', 'blue')
single = (5,) # a trailing comma is required for a single-item tuple
Indexing and Slicing
colors = ('red', 'green', 'blue', 'yellow')
print(colors[0]) # red
print(colors[-1]) # yellow
print(colors[1:3]) # ('green', 'blue')
Immutability: The Key Property
point = (4, 5)
point[0] = 10 # TypeError: 'tuple' object does not support item assignment
If you need to change a tuple's contents, you must create a new tuple, typically by converting to a list, modifying it, and converting back.
point = (4, 5)
point_list = list(point)
point_list[0] = 10
point = tuple(point_list) # (10, 5)
Tuple Unpacking
point = (4, 5)
x, y = point
print(x, y) # 4 5
# Common pattern: a function returning multiple values as a tuple
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([3, 7, 1, 9])
Tuple Methods
Tuples support far fewer methods than lists, since their contents cannot change.
numbers = (1, 2, 2, 3, 2)
print(numbers.count(2)) # 3
print(numbers.index(3)) # 3
Tuple vs List
| Tuple | List |
|---|---|
| Immutable | Mutable |
| Written with () | Written with [] |
| Slightly faster, hashable | Slightly slower, not hashable |
| Can be used as a dictionary key | Cannot be used as a dictionary key |
Common Interview Questions
Why would you choose a tuple over a list?
When the data should not change after creation, such as coordinates or a fixed configuration, and because tuples are slightly more memory efficient and can be used as dictionary keys.
Can a tuple contain a mutable object like a list?
Yes. The tuple itself cannot be resized or reassigned, but if one of its elements is a mutable object like a list, that inner list can still be modified.
FAQ
Frequently Asked Questions
What is the main difference between a tuple and a list?
A tuple is immutable, meaning it cannot be changed after creation, while a list is mutable. Tuples use parentheses and lists use square brackets.
Can you modify a tuple after creating it?
No, not directly. You would need to convert it to a list, make the change, and convert it back to a tuple.
Why can a tuple be used as a dictionary key but a list cannot?
Dictionary keys must be hashable, meaning their value cannot change. Tuples are immutable and hashable, while lists are mutable and therefore not hashable.
How do you create a tuple with only one element?
Include a trailing comma, such as (5,). Without the comma, Python treats it as a plain integer in parentheses, not a tuple.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin