Learn by reading through in order

How to Use Python Tuples (tuple)

Learn the Python tuple — the immutable cousin of list — and pick up unpacking and dict-key tricks right in your browser.

What a tuple is — the "can't be changed" version of a list

A tuple (tuple) lets you hold several values together in a single variable. It's very similar to the list you saw in the previous article, but the key difference is that once you create one, you can't change its contents.

If a list is a container you plan to rewrite (like a shopping list), a tuple is a container you decide once and leave alone — think coordinates (x, y) or an RGB color like (255, 0, 0).

list vs tuple
list[1, 2, 3]mutabletuple(1, 2, 3)immutabledeclarepropertydeclareproperty

Only the type of brackets and whether it can change differ; the job of grouping several values together is the same.

If it changes, use list; if it stays put, use tuple is a simple rule of thumb.

To declare one, just wrap values in ( and ), separated by commas. Where a list uses [ ], a tuple uses ( ).

Accessing elements (indexing and slicing) works exactly the same as with a list: [0] for the first, [-1] for the last, [1:3] for a slice.

# Declaring a tuple
fruits = ("apple", "banana", "lemon")

print(fruits)        # ('apple', 'banana', 'lemon')
print(type(fruits))  # <class 'tuple'>
print(fruits[0])     # apple
print(fruits[-1])    # lemon
print(fruits[1:])    # ('banana', 'lemon')  ← a slice is also a tuple

# Mixed types are fine
profile = ("Alice", 28, 165.5)

A single-element tuple needs a trailing comma

If you write fruits = ("apple"), that's a string, not a tuple. ( ) is also used as the math grouping parentheses, so Python can't tell them apart when there's only one value inside.

To make a one-element tuple, add a trailing comma like ("apple",).

Python also lets you drop the parentheses entirely and write something like 1, 2, 3 as a tuple, but for readability you should still keep the ( ).

Declare a tuple and read values out of it.

① Create colors = ("red", "green", "blue") and use print() to display the first and last colors.

② Print colors[0:2] and then use type() to confirm that a slice is still a tuple.

(If your code runs correctly, the explanation will appear.)

Python Editor

Run code to see output

Immutable — "rewriting an element" and "swapping the box" are different

A tuple is immutable (unchangeable). Once you've created it, you can't overwrite, add, or remove its elements.

The thing people most often trip over here is the difference between "changing an element" and "putting a brand-new tuple into the variable." The first is a no-go; the second is fine.

"Rewrite an element" vs "Swap the box"
t = (1,2,3)t[0] = 9TypeErrort = (1,2,3)t = (4,5,6)points tonew tupleelementfailsreassignworks

t[0] = 9 tries to change the contents directly, so it's an error.

t = (4, 5, 6) just points the variable at a new tuple, so it's fine.

Let's confirm that trying to rewrite a tuple's element really does raise an error.

① Run the code below as-is. You should see a TypeError.

② Once you've seen the error, comment out the my_tuple[0] = 9 line (add a # at the start), then add my_tuple = (9, 2, 3) instead and print my_tuple. That one won't error out.

Python Editor

Run code to see output

Concatenation and methods (count / index)

You can build a new tuple by joining two tuples together. Using + to concatenate returns a brand-new tuple.

There aren't many methods — the two you'll actually use are count and index. They feel the same as on a list.

What you wantHow to write itKey point
Concatenatet1 + t2Returns a new tuple; the originals don't change
Add one elementt + (x,)Don't forget the trailing comma
Count valuest.count(x)How many times x appears
Find a positiont.index(x)The first position where x appears
fruits = ("apple", "banana", "lemon")

# Concatenate (returns a new tuple)
more = fruits + ("grape",)
print(more)            # ('apple', 'banana', 'lemon', 'grape')
print(fruits)          # ('apple', 'banana', 'lemon')  ← original is unchanged

# count and index
nums = (1, 2, 2, 3, 2, 4)
print(nums.count(2))   # 3 (there are three 2s)
print(nums.index(3))   # 3 (3 is the 4th item = index 3)

# Converting between list and tuple
list_fruits  = list(fruits)   # ['apple', 'banana', 'lemon']
tuple_fruits = tuple(list_fruits)  # ('apple', 'banana', 'lemon')

If you want to keep editing, convert to a list

A tuple is for "values you barely move," so it's a poor fit when you need to add and remove items repeatedly. The standard pattern is convert to a list with list(t) → edit → convert back with tuple(...) if needed.

Pull information out of a tuple of weekdays.

days = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") is given.

① Use days.index("Fri") and print() to display what position "Fri" is at.

② Create weekend = ("Sat", "Sun"), then print days + weekend to see what concatenation produces (it's fine if some items appear twice).

Python Editor

Run code to see output

Unpacking and dict keys — what only tuples can do

There are two things a tuple can do that a list can't: unpacking and being used as a dict key.

Unpacking — break it apart into several variables in one line
coord=(135, 35)lng, lat= coordlng=135lat=35assignunpack

Put comma-separated variable names on the left-hand side and each element of the tuple goes straight into its own variable.

The match is position-by-position, and the number of variables has to equal the number of elements.

# Unpacking
coord = (135.0, 35.0)
longitude, latitude = coord
print(longitude)   # 135.0
print(latitude)    # 35.0

# You can also unpack from a literal
r, g, b = (255, 128, 0)
print(r, g, b)     # 255 128 0

# A common trick: swap two values in one line
a, b = 1, 2
a, b = b, a
print(a, b)        # 2 1

A function's return value is also a tuple (preview)

A Python function can return multiple values with return a, b, and those are automatically packed into a tuple. On the caller side, writing x, y = func() unpacks them into two variables. The functions chapter comes later, so we won't go deep here — but remembering "multiple return values and unpacking go together" will make that chapter click faster.

Unpack an RGB color into its pieces.

rgb = (200, 100, 50) is given.

① Using unpacking, pull the red / green / blue values into three variables r, g, b.

② Use print() to display r, g, and b on one line each.

Python Editor

Run code to see output

The other strength is that a tuple can be used as a dict key. A list can't be a dict key, but a tuple can. This is useful when you want to use a combination of several values as a single key — like mapping coordinates to a country name.

A tuple can be a dict key
(135,35)tupleuse asdict keyOK[135,35]listuse asdict keyTypeError
# A dict with tuple keys
country = {
    (135.0, 35.0): "Japan",
    (-77.0, 38.9): "USA",
}
print(country[(135.0, 35.0)])   # Japan
print(country.get((0.0, 0.0), "Unknown"))   # Unknown

# A list can't be a key
# bad = {[135.0, 35.0]: "Japan"}  # TypeError: unhashable type: 'list'

Build a dict that ties coordinates to city names.

① Create a dict cities where the key (35.0, 139.0) maps to "Tokyo", and the key (34.7, 135.5) maps to "Osaka".

② Print cities[(35.0, 139.0)] to confirm you can pull Tokyo back out.

③ Look up (0.0, 0.0) with .get(), passing "Unknown" as the second argument, and confirm that it works safely even for a missing key.

Python Editor

Run code to see output

In this article, framed around differences from a list, you learned how to declare a tuple, what immutability means, concatenation and count / index, and unpacking and dict keys.

QUIZ

Knowledge Check

Answer each question one by one.

Q1Which of the following is declared correctly as a tuple?

Q2Given t = (1, 2, 3), which one does not raise an error?

Q3For coord = (135.0, 35.0), what's the simplest way to pull its two values into variables lng and lat?