Working with Text and Collections

Tuples: Fixed Collections

6 min read

Some things shouldn't change

A GPS coordinate for the Eiffel Tower is always (48.8584, 2.2945). The three channels in an RGB color value for pure red are always (255, 0, 0). The days of the week are always the same seven names.

When data represents a fixed, well-defined group of values that should never be modified, Python gives you a better tool than a list: the tuple.


What is a tuple?

A tuple is an ordered collection of values — like a list — but with one key difference: tuples cannot be changed after they are created. This property is called immutability.

You write a tuple with parentheses () instead of square brackets []:

coordinate = (37.7749, -122.4194)
print("Coordinate:", coordinate)
# (37.7749, -122.4194)

The parentheses are actually optional — Python treats comma-separated values as a tuple even without them. This is called packing:

blog_hits = 'Myblog.com', 50, '20-Oct-2013'
print(blog_hits)
# ('Myblog.com', 50, '20-Oct-2013')
print(type(blog_hits))
# <class 'tuple'>

The parentheses are still the clearer, more common way to write a tuple — but you'll see the bare comma-separated form in real code, especially when returning multiple values from a function.


Accessing tuple items

Tuples use the same index-based access as lists:

coordinate = (37.7749, -122.4194)
print("Latitude:", coordinate[0])   # 37.7749
print("Longitude:", coordinate[1])  # -122.4194

Unpacking a tuple

A particularly useful feature of tuples is unpacking — assigning each item in a tuple to its own named variable in a single line:

coordinate = (37.7749, -122.4194)
latitude, longitude = coordinate

print("Latitude:", latitude)    # 37.7749
print("Longitude:", longitude)  # -122.4194

The number of variables on the left must match the number of items in the tuple. This makes code much more readable than constantly writing coordinate[0] and coordinate[1].

Unpacking works for any size tuple:

rgb_red = (255, 0, 0)
r, g, b = rgb_red
print(f"Red: {r}, Green: {g}, Blue: {b}")
# Red: 255, Green: 0, Blue: 0

Tuples are immutable — on purpose

Try to change a value in a tuple and Python raises an error:

immutable_tuple = (1, 2, 3)
immutable_tuple[0] = 5  # This would raise an error!
# TypeError: 'tuple' object does not support item assignment

This is a feature, not a flaw. When something is a tuple, anyone reading the code knows immediately: these values are fixed and should not change. It prevents accidental modification of data that should stay constant.


Common tuple operations

Tuples support many of the same operations as lists — just without anything that would modify them in place.

ExpressionResultDescription
len((1, 2, 3))3Number of items
(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Concatenation — joins two tuples into a new one
(1, 2, 3, 4, 5, 6)[2:5](3, 4, 5)Slicing — works exactly like list and string slicing
a = (1, 2, 3, 4, 5, 6)
print(len(a))        # 6
print(a + (7, 8))    # (1, 2, 3, 4, 5, 6, 7, 8)
print(a[2:5])         # (3, 4, 5)

Concatenating with + and slicing both return a new tuple — they never modify the original, since tuples can't be modified at all.


A list of tuples: grouped records

A common real-world pattern is a list where each item is a tuple representing one fixed record — like a name paired with a color code:

colors = [
    ('red', '#FF0000'),
    ('green', '#00FF00'),
    ('blue', '#0000FF'),
]

for name, hex_code in colors:
    print(f"{name}: {hex_code}")

Output:

red: #FF0000
green: #00FF00
blue: #0000FF

Each tuple is a fixed pair that shouldn't change, while the outer list can still grow if you need to add more records. This pattern shows up constantly — rows from a database query, coordinate paths, and labeled data points are all commonly represented as lists of tuples.


Converting between tuples and lists

If you realize you need to modify a tuple's contents, you can convert it to a list, make your changes, and convert it back:

immutable_tuple = (1, 2, 3)
print("Tuple values:", immutable_tuple)

mutable_copy = list(immutable_tuple)   # convert to list
mutable_copy.append(4)                 # modify the list
print("Modified copy:", mutable_copy)
# Modified copy: [1, 2, 3, 4]

back_to_tuple = tuple(mutable_copy)    # convert back if needed
print("Back to tuple:", back_to_tuple)

When to use a tuple vs. a list

Use a list when...Use a tuple when...
The collection might grow or shrinkThe collection is fixed in size
You'll add, remove, or sort itemsThe values should never be changed
Order matters but contents varyOrder matters and contents are constant
Storing a dynamic datasetStoring configuration, coordinates, or settings

Examples of natural tuples:

  • GPS coordinates: (latitude, longitude)
  • RGB color values: (red, green, blue)
  • A date: (year, month, day)
  • Screen dimensions: (width, height)
  • A database record row: (id, name, email)

Tuples can hold any types

Like lists, tuples can hold a mix of data types:

person = ("Priya", 22, True, "student")
name, age, is_enrolled, role = person
print(f"{name} is {age} years old and is a {role}.")
# Priya is 22 years old and is a student.

A note on single-item tuples

A quirk worth knowing: a tuple with just one item needs a trailing comma, otherwise Python reads the parentheses as just grouping an expression:

not_a_tuple = (42)       # this is just the integer 42
actual_tuple = (42,)     # this IS a tuple with one item

print(type(not_a_tuple))   # <class 'int'>
print(type(actual_tuple))  # <class 'tuple'>

A preview: returning multiple values from a function

One of the most common uses of tuple packing and unpacking is returning more than one value from a function. You'll cover functions properly in a later lesson, but the pattern relies entirely on what you just learned:

def points(x):
    y = 1 - x
    return x, y   # packs x and y into a tuple

result = points(0.3)
print(result)         # (0.3, 0.7)

x_val, y_val = points(0.3)   # unpacks the returned tuple
print(x_val, y_val)          # 0.3 0.7

return x, y packs both values into a tuple automatically. The caller can then either use the tuple as-is or unpack it straight into separate variables — the same unpacking syntax from earlier in this lesson.


Key takeaway

Tuples are ordered, immutable collections written with parentheses. Their inability to change is intentional — it signals that the data is fixed and protects it from accidental modification. Use tuples for coordinates, configuration values, colors, dates, and other fixed groups of related values. Unpacking makes working with tuples clean and readable.

What's next?

Lists and tuples store values in order by position. But what if you want to look things up by a name rather than a position? Python dictionaries let you map keys to values — like a real-world dictionary maps words to definitions.