Tuples in Python

A tuple is an ordered collection of items, very similar to a list. You create one by placing comma-separated values inside parentheses. The one big difference from a list is that a tuple is immutable — once created, it cannot be changed. That single property shapes almost everything else about how and when you use tuples.

Creating a Tuple

A tuple is written using round brackets () instead of the square brackets [] used for lists. Tuples can hold items of different data types, and they keep the order in which the items were inserted.

# a simple tuple
point = (3, 4)
print(point)

# a tuple can hold mixed data types
person = ("Alice", 30, "Delhi")
print(person)

# parentheses are optional, but recommended for clarity
coordinates = 10, 20
print(coordinates)

Indexing and Slicing

Tuples support indexing and slicing exactly the same way lists do. Indexing starts at 0, and negative indexes count from the end. Slicing returns a new tuple containing the selected range.

fruits = ("apple", "banana", "cherry", "mango")

print(fruits[0])       # apple
print(fruits[-1])      # mango
print(fruits[1:3])     # ('banana', 'cherry')
print(len(fruits))   # 4

Tuples Are Immutable

This is the key idea to take away from this page. On the previous page you saw that a list can be changed in place with my_list[0] = "new value". Try the same thing on a tuple and Python raises a TypeError, because tuples do not allow item assignment after creation.

numbers_list = [1, 2, 3]
numbers_list[0] = 99       # works fine - lists are mutable
print(numbers_list)     # [99, 2, 3]

numbers_tuple = (1, 2, 3)
numbers_tuple[0] = 99     # raises TypeError: 'tuple' object does not support item assignment

Note: Immutability applies to the tuple's structure, not necessarily to what's inside it. A tuple that contains a list, like ([1, 2], "x"), cannot swap out that list for a different object, but the list itself can still be modified in place.

Why Choose a Tuple Over a List?

Since tuples do so much less than lists, it may not be obvious why they exist. A few practical reasons:

  • Fixed collections: when a group of values should never change (like a geographic coordinate or an RGB color), a tuple communicates that intent directly in the code.
  • Safety: immutability prevents accidental modification elsewhere in a large program.
  • Performance: tuples are generally slightly faster to create and iterate over than lists.
  • Usable as dictionary keys: because tuples are immutable, they are hashable, so they can be used as keys in a dictionary or stored inside a set. Lists cannot be used this way, since lists are mutable and therefore unhashable.
# a tuple can be a dictionary key, a list cannot
locations = {(28.6, 77.2): "Delhi", (19.0, 72.8): "Mumbai"}
print(locations[(28.6, 77.2)])   # Delhi

# this would fail: locations[[28.6, 77.2]] = "Delhi"  -> TypeError: unhashable type: 'list'

Tuple Unpacking

Tuple unpacking lets you assign each item of a tuple to its own variable in a single line. The number of variables on the left must match the number of items in the tuple.

point = (3, 4)
x, y = point
print(x)   # 3
print(y)   # 4

# works for functions that return multiple values too
def min_max(values):
    return min(values), max(values)

lowest, highest = min_max([4, 9, 1, 7])
print(lowest, highest)   # 1 9

The Single-Element Tuple Gotcha

To create a tuple with only one item, a trailing comma is required. Without the comma, Python treats the parentheses as ordinary grouping, and the result is not a tuple at all.

not_a_tuple = (5)
print(type(not_a_tuple))    # <class 'int'>

single_tuple = (5,)
print(type(single_tuple))  # <class 'tuple'>

Note: The comma is what actually makes a tuple in Python, not the parentheses. 5, alone is already a valid one-element tuple; the parentheses just make the intent readable.

Tuple Methods: count() and index()

Because tuples cannot be modified, they only have two built-in methods — there is nothing to add, remove, sort, or reverse.

MethodDescription
.count(value)Returns how many times value appears in the tuple.
.index(value)Returns the index of the first occurrence of value; raises ValueError if not found.
numbers = (1, 2, 2, 3, 2, 4)

print(numbers.count(2))   # 3
print(numbers.index(3))   # 3 (first position where 3 appears)
Next steps: Head to Sets to learn about unordered collections of unique values.