Data StructuresBeginner7 min23 / 63

Tuples

Learn about tuples — Python's locked-in, ordered sequences — and why their immutability makes them surprisingly powerful.

Imagine you're filling out a form with your date of birth. Once you write it down, it doesn't change — ever. That's the idea behind a tuple. A tuple is an ordered collection of values, just like a list, but with one key difference: you cannot change it after you create it. That quality is called immutability, and it turns out to be really useful.

See it in action

Visual walkthrough1 / 6
1

Tuples: Sealed Boxes of Data

A tuple is an ordered collection of values — like a list, but locked. Once you create it, the contents can never change.

Immutability is a feature: it guarantees the data stays exactly as you left it.

#Creating a Tuple

You can create a tuple with parentheses (), separating values with commas. You can also skip the parentheses entirely — Python recognizes the commas and builds a tuple anyway.

Both styles produce a tuple. The commas are what really matter.
# With parentheses
coordinates = (40.7128, -74.0060)
print(coordinates)

# Without parentheses — still a tuple!
rgb = 255, 128, 0
print(rgb)
print(type(rgb))
Common mistake

The Single-Element Trap

Want a tuple with just one item? You must add a trailing comma, or Python treats it as a plain value in parentheses.

``python not_a_tuple = (42) # This is just the number 42 is_a_tuple = (42,) # This IS a tuple print(type(not_a_tuple)) # <class 'int'> print(type(is_a_tuple)) # <class 'tuple'> ``

That lonely trailing comma makes all the difference.

#Accessing Elements

Tuples are indexed exactly like lists — the first item is at position 0. You can also use negative indexes to count from the end.

Square brackets and indexes work the same as with lists.
person = ("Alice", 30, "engineer")

print(person[0])   # first element
print(person[-1])  # last element

#Tuples Are Immutable

Once a tuple is created, you cannot add, remove, or change its values. Try to do so and Python will raise an error. This is a feature, not a bug — it means you can trust the data inside a tuple to stay exactly as you left it.

Python enforces immutability by raising a TypeError.
point = (3, 7)
point[0] = 10  # This will cause an error!
Think of it like

Tuple vs List: Sealed Box vs Open Bag

Think of a list as a reusable shopping bag — you can toss items in, take them out, and rearrange freely. A tuple is a sealed box from the factory — everything inside is fixed and guaranteed. Use a sealed box when the contents should never change (like GPS coordinates, RGB colors, or a date).

#Unpacking a Tuple

Unpacking lets you assign each element of a tuple to its own variable in one clean line. This is one of the most satisfying features in Python.

One line unpacks all three values at once.
color = (255, 128, 0)

r, g, b = color

print(r)  # 255
print(g)  # 128
print(b)  # 0

#Swapping Variables in One Line

Tuple unpacking enables a Python party trick: swapping two variables without a temporary helper variable. In most languages this takes three lines; in Python, it takes one.

Python packs the right side into a tuple, then unpacks it onto the left.
a = "hello"
b = "world"

a, b = b, a

print(a)  # world
print(b)  # hello

#Returning Multiple Values from a Function

A function can only return one thing — but that one thing can be a tuple containing several values. This is the standard Python pattern for returning multiple results.

The function returns a two-element tuple; unpacking catches both values.
def min_max(numbers):
    return min(numbers), max(numbers)

scores = [72, 95, 88, 61, 100]
lowest, highest = min_max(scores)

print("Lowest:", lowest)
print("Highest:", highest)

#When to Use Tuples vs Lists

Here's a simple rule of thumb:

  • Use a list when the data might change (a to-do list, a shopping cart).
  • Use a tuple when the data is fixed (a coordinate pair, a configuration setting, a date).

Tuples also unlock one superpower lists don't have: you can use them as dictionary keys, because they are immutable.

Lists cannot be dictionary keys — tuples can, because they are hashable.
# Tuples as dictionary keys
location_names = {
    (40.7128, -74.0060): "New York City",
    (51.5074, -0.1278): "London",
}

print(location_names[(40.7128, -74.0060)])

#Useful Tuple Methods

Tuples have only two built-in methods — count() and index() — because there's not much you can do to an immutable sequence.

count() tallies occurrences; index() finds the first position of a value.
grades = ("A", "B", "A", "C", "A")

print(grades.count("A"))    # how many times does 'A' appear?
print(grades.index("C"))    # at which index is 'C' ?
Tip

Tuples Are Slightly Faster Than Lists

Because Python knows a tuple can never change, it can store and access tuples more efficiently than lists. For large, fixed datasets this small speed boost can add up. When in doubt, if you know the data won't change, reach for a tuple.

Quick check

Which of the following correctly creates a tuple containing exactly one element — the number 5?

Key takeaways

  • A tuple is an ordered, immutable sequence — once created, its contents cannot change.
  • A single-element tuple requires a trailing comma: `(42,)` not `(42)`.
  • Unpacking lets you assign tuple elements to variables in one line: `a, b, c = my_tuple`.
  • Functions can return multiple values by returning a tuple, which you can unpack at the call site.
  • Tuples can be used as dictionary keys; lists cannot — because tuples are immutable (hashable).
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
person = ("Alice", 30, "engineer")
print(person[0])
print(person[-1])
Fix the bug#2

This code tries to change the first element of a tuple, but it has a bug. What is wrong?

fix-bug
point = (3, 7)
point[0] = 10
print(point)
Fill in the blank#3

Complete the code so it unpacks the tuple and prints each value on its own line.

color = (255, 128, 0)
r, , b = color
print(r)
print(g)
print(b)
Reorder the lines#4

Put these lines in the right order to swap two variables using tuple unpacking, then print them.

1
print(a)
2
print(b)
3
a = "hello"
4
a, b = b, a
5
b = "world"
Your turn
Practice exercise

Write a function called summarize that takes a list of numbers and returns a tuple containing three values: the minimum, the maximum, and the average (rounded to 2 decimal places). Then unpack and print all three values with clear labels.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable