Skip to main content

What are Tuples?

Tuples are very similar to lists, but with one major difference: they cannot be changed once created. While lists use square brackets [], tuples use parentheses ().

Think of a tuple like:

  • GPS Coordinates (latitude, longitude)
  • Calendar Dates (year, month, day)
  • RGB Color Codes (255, 0, 128)

If you have fixed data that should never be accidentally altered or overwritten while your program runs, use a tuple instead of a list.

Creating Tuples

# A standard tuple
dimensions = (1920, 1080)

# Accessing items works exactly like lists (starting at index 0)
print(dimensions[0]) # Output: 1920
print(dimensions[1]) # Output: 1080

:::warning Single-Item Tuple Trap If you want to create a tuple containing only one item, you must place a comma after the item! Otherwise, Python treats the parentheses as simple math brackets around a regular number or string:

# Wrong - Python treats this as an integer
not_a_tuple = (42)
print(type(not_a_tuple)) # <class 'int'>

# Right - comma tells Python it is a tuple
single_item = (42,)
print(type(single_item)) # <class 'tuple'>

:::

Why Use Tuples Instead of Lists?

  1. Safety: Prevents accidental modification of fixed configuration values or database records.
  2. Speed: Tuples are lightweight and read faster by Python than dynamic lists.
  3. Dictionary Keys: Because tuples cannot be changed, they can be used as keys inside Python Dictionaries (lists cannot!).

Tuples Cannot Be Modified (Immutability)

If you try to append, remove, or change an item inside a tuple, Python throws an error immediately:

coordinates = (10.5, 20.8)
# coordinates[0] = 15.0 # TypeError: 'tuple' object does not support item assignment

Tuple Methods

Because tuples cannot be changed, they only have two built-in inspection methods:

grades = ("A", "B", "A", "C", "A")

print(grades.count("A")) # Count occurrences: 3
print(grades.index("B")) # Find index position: 1

Common Beginner Mistakes

Trying to append to a tuple
# Wrong - tuples do not have .append() or .remove()
colors = ("red", "blue")
colors.append("green") # AttributeError!

# Right - if you need to add items regularly, use a list instead
colors_list = ["red", "blue"]
colors_list.append("green")

What's Next?

Let's explore how to pack multiple variables into a tuple and unpack them in a single line.