📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

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

TupleList
ImmutableMutable
Written with ()Written with []
Slightly faster, hashableSlightly slower, not hashable
Can be used as a dictionary keyCannot 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.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles