Python Tuples

A tuple is a sequence similar to a Python list. The key difference between the two is that we cannot change the items of a tuple once it is created.

When to use a tuple: Use it for data that shouldn't change, like an order's ID, date and shipping address (111, "2026-08-09", "California"). This keeps the data protected from accidental changes.

When to use a list: Use it when data needs to change, like a shopping cart where products are added, removed and updated before checkout.


Creating a Tuple

A tuple is created by placing items inside parentheses (), separated by commas. For example,

# Empty tuple
numbers = ()
print(numbers)

# Tuple having data of the same type
odd_nums = (1, 3, 5, 7)
print(odd_nums)

# Tuple having mixed data types
details = (111, "2026-08-09", "California")
print(details)

We can also create a tuple using the tuple() function. The function converts an iterable (such as a string or a list) into a tuple. For example,

vowels = "aeiou"

# Convert a string to a tuple
vowels_tuple = tuple(vowels)
print(vowels_tuple)

Output

('a', 'e', 'i', 'o', 'u')

Tuple Unpacking

In the above programs, we grouped individual items into a single tuple variable. This is called packing a tuple. We can also extract individual items from a tuple and assign them to variables in a single line. This is called unpacking.

id, date, location = (111, "2026-08-09", "California")

print(id)         # Output: 111     
print(date)       # Output: 2026-08-09
print(location)   # Output: California

Note: It's not necessary to use () to create tuples. We just need to separate items with commas. The only reason we use parentheses is to make the code more readable.

# A tuple of three numbers
numbers = 1, 2, 3

# A tuple with a single item
number = (5,)

# A tuple with a single item
number = 5,

# An integer (not a tuple)
number = 5

# An integer (not a tuple)
number = (5)

In the last statement, number = (5) is equivalent to number = 5, as the parentheses are optional when creating a tuple. Therefore, to create a tuple of a single item, we must add a comma after the item.


Accessing Tuple Items

The items in a tuple are ordered and each item is associated with a number, known as an index.

The index of the first item is 0, the index of the second item is 1 and so on.

Tuple Indexing
Indices of Tuple Items

We use these indices to access tuple items. For example,

languages = ('Python', 'Swift', 'C++')

# Access the first item
print(languages[0])   # Python

# Access the third item
print(languages[2])   # C++
Access Tuple Items
Access Tuple Items

Negative Indexing

Python also supports negative indexing. The index of the last item is -1, the index of the second last item is -2 and so on.

languages = ("Python", "Swift", "C++")

# Access the last item
print(f"languages[-1] = {languages[-1]}")

# Access the third last item
print(f"languages[-3] = {languages[-3]}") 

Output

languages[-1] = C++
languages[-3] = Python

We can also extract a portion of a tuple (or any other sequence) easily using the slicing operator :. Learn more on the dedicated tutorial: Python Slicing.


Tuples are Immutable

Python tuples are immutable (unchangeable). We cannot add, change or delete items of a tuple.

If we try to modify a tuple, we will get an error. For example,

cars = ("BMW", "Tesla", "Ford", "Toyota")

# Trying to change the first item
cars[0] = "Nissan"    # Error
       
print(cars)

Output

Traceback (most recent call last):
  File "<main.py>", line 4, in <module>
TypeError: 'tuple' object does not support item assignment

This is the reason why tuples keep data protected from accidental changes.


Deleting a Tuple

We cannot delete items of a tuple but we can delete the tuple itself using the del keyword.

cars = ("BMW", "Tesla", "Ford", "Toyota")

# Deleting the cars tuple
del cars

print(cars)

Output

Traceback (most recent call last):
  File "<main.py>", line 6, in <module>
NameError: name 'cars' is not defined.

Note: If a tuple contains a mutable object like a list as an item, that object's contents can still be changed. This is because tuples protect which objects it refers to, but not what's inside those objects.

details = ("Alice", [111, "2026-08-09", "California"])

# Emptying the list inside the tuple
details[1].clear()

print(details) # Output: ('Alice', [])

Here, the tuple contains a string "Alice" and a list [111, "2026-08-09", "California"]. When we clear the list inside the tuple, it still refers to the same two objects: the string "Alice" and the list (which is now empty). That's why this program works without any errors.


Python Tuple Length

Built-in functions such as enumerate(), len(), max(), min(), sorted() etc. are commonly used with tuples to perform different tasks. You can find all the built-in functions in Python here.

One built-in function worth memorizing is len(). The len() function returns the number of items in a tuple.

cars = ("BMW", "Tesla", "Ford", "Toyota")
print(f"Total Items = {len(cars)}") 
       
# Output: Total Items = 4

Tuple Membership Test

We can test if an item exists in a tuple or not, using the in keyword.

cars = ("BMW", "Tesla", "Ford", "Toyota")

result = "BYD" in cars
print(result)    # False

result = "Ford" in cars
print(result)    # True

Iterating Through a Tuple

Since a tuple is an ordered sequence of items, we can iterate through items of a tuple using a for loop.

cars = ("BMW", "Tesla", "Ford", "Toyota")

for car in cars:
    print(car)
Did you find this article helpful?