Python tuples

grouping objects into a collection of ordered but unchangeable items
2025-11-05 14:19
// updated 2025-11-06 18:58

In Python, we have a special kind of data structure called a tuple which functions like a list, except that its items cannot change once we have defined them:

my_tuple = ('a', 2, 'd', False)

Accessing tuple items

Similar to a string, we can access the elements of a tuple and information about the tuple itself, but we cannot alter a tuple's items (via the familiar square bracket index notation):

my_tuple = ("a", 2, "d", False, "d")

print(my_tuple[1])
# 2

print(len(my_tuple))
# 5

print(my_tuple.index("d"))
# 2 (the first instance)

my_tuple[5] = "e"
# TypeError: 'tuple' object does not support item assignment

my_tuple[2] = "e"
# TypeError: 'tuple' object does not support item assignment

We can thus think of a tuple as a list that behaves like a string!

Summary

We can think of a tuple as:

  • an ordered collection of one or more immutable (unchangeable) items:
    • each referenced by a numerical index
      • the value can take on any type
    • incapable of update
  • denoted by brackets ( value1, ..., valueN )

Applications

Great uses for tuples include collections of:

  • objects whose values should not change but their order does matter, e.g.:
    • the days of the week
    • the months of the year
⬅️ older (in textbook-python)
🐍 Python lists
newer (in textbook-python) ➡️
Python dictionaries 🐍
⬅️ older (in code)
🐍 Python lists
newer (in code) ➡️
Python dictionaries 🐍
⬅️ older (posts)
🐍 Python lists
newer (posts) ➡️
Python dictionaries 🐍