VMware ESXi and vSphere Cluster Management

What Are Tuples in Python?

Learn what Python tuples are, how to create and index them, why they are immutable, and when to use tuples instead of lists.

A tuple is an ordered Python collection that stores multiple values under one variable name. Like a list, a tuple is a sequence: its values have a specific order, can be accessed by position, and can be repeated.

The main difference is that a tuple is immutable, which means it cannot be changed after it is created. Tuples are useful when a group of values should stay fixed.

Tuples and Sequences

A sequence is an ordered collection whose values can be accessed by position. Python lists and tuples are both common sequence types.

scores = (2, 4, 2, 5)
print(scores)
(2, 4, 2, 5)

This tuple stores four values in one variable. The value 2 appears twice, so repeated values are allowed.

Tuple Immutability

Immutable means unable to be changed after creation. You cannot replace, add, remove, or reorder the items in a tuple. A useful beginner mental model is a tuple as a read-only list, although tuples have their own role and behavior in Python.

Lists are mutable, so their items can be edited. Tuples are suitable for fixed data that should not be changed accidentally, such as a coordinate or a fixed group of related values.

FeatureTupleList
Literal syntax("red", "blue")["red", "blue"]
Can items be changed after creation?No; tuples are immutable.Yes; lists are mutable.
Best use caseFixed collections of related values.Collections that need to grow, shrink, or be edited.
Indexing and iterationSupported.Supported.

Creating Tuples

The usual tuple syntax uses comma-separated values inside parentheses. The commas are what establish the tuple; parentheses mainly group the values and make the intent clear.

colors = ("red", "green", "blue")
measurements = (10, 2.5, "cm")
print(colors)
print(measurements)

A tuple can contain different data types, including integers, floating-point numbers, and strings.

Common Tuple Forms

FormMeaningKey detail
(1, 2, 3)Multiple-item tupleValues are separated by commas.
()Empty tupleContains no items.
("ready",)One-item, or singleton, tupleThe trailing comma is required.
1, 2, 3Tuple without parenthesesThe commas still create a tuple, but parentheses often improve readability.

Empty and Singleton Tuples

An empty tuple has no values:

empty = ()
print(empty)

A singleton tuple contains exactly one item. It must include a trailing comma:

not_a_tuple = ("Python")
one_item = ("Python",)

print(type(not_a_tuple))
print(type(one_item))

("Python") is just a parenthesized string. ("Python",) is a tuple because of the comma.

Accessing Tuple Items

An index is a numeric position used to access an item in a sequence. Python uses zero-based indexing, so the first item has index 0, the second has index 1, and so on.

coordinates = (12, 8, 3)

print(coordinates[0])
print(coordinates[1])
12
8

Negative indexes count from the end. The index -1 refers to the last item, -2 to the item before it, and so on.

coordinates = (12, 8, 3)

print(coordinates[-1])
print(coordinates[-2])
3
8

Slicing a Tuple

Slicing obtains a portion of a sequence. The expression tuple[start:stop] includes the item at start but stops before stop.

numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
(20, 30, 40)

The slice produces a new tuple. It does not modify the original tuple.

Attempting to Modify a Tuple

Item assignment means replacing a value at a particular index, using syntax such as collection[0] = value. Item assignment is not supported for tuples.

colors = ("red", "green", "blue")
colors[0] = "yellow"
TypeError: 'tuple' object does not support item assignment

Python raises a TypeError because this operation is not supported by the tuple type. You can read an item and assign that value to another variable, but that does not change the tuple:

colors = ("red", "green", "blue")
first_color = colors[0]
print(first_color)
red

The tuple itself remains unchanged. If the collection must be edited, use a list or create a new tuple with the desired values.

colors = ("red", "green", "blue")
updated_colors = ("yellow",) + colors[1:]
print(updated_colors)
("yellow", "green", "blue")

Iterating Over a Tuple

Iteration means visiting each item in a collection, commonly with a for loop. A loop reads tuple values in their stored order without modifying the tuple.

labels = ("start", "middle", "end")

for label in labels:
    print(label)
start
middle
end

The loop variable label receives each value one at a time. It does not make the tuple mutable.

When to Use Tuples

Choose a tuple when the collection represents a fixed group of related values. Examples include:

  • Days in a workweek: ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  • A coordinate: (40.7128, -74.0060)
  • A fixed measurement: (1920, 1080)
  • A small group of values that should be passed together without being edited
workweek = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
location = (40.7128, -74.0060)

print(workweek[0])
print(location[1])

Choose a list when the collection must change, such as when items will be added, removed, or replaced. Choose a tuple when the collection should remain fixed.

Common Tuple Problems

TypeError During Item Assignment

Symptom: An error occurs when code assigns to tuple[index].

Cause: The code is trying to modify an immutable tuple.

Resolution: Use a list if the collection must be edited, or create a new tuple containing the desired values.

A Supposed One-Item Tuple Is Not a Tuple

Symptom: A parenthesized value behaves like a normal value.

Cause: The trailing comma was omitted.

Resolution: Write the singleton tuple with a comma after its item:

item = ("only value",)

The First Item Is Not Returned

Symptom: Index 1 returns the second item instead of the first.

Cause: Python indexes sequences from 0.

Resolution: Use index 0 for the first item.

IndexError While Accessing an Item

Symptom: Python raises an IndexError.

Cause: The requested index is outside the tuple's valid positions.

Resolution: Check the tuple length. For a tuple with n items, valid positive indexes range from 0 through n - 1. You can also use applicable negative indexes, beginning at -1.

Key Points

  • A tuple is an ordered Python collection of values.
  • Tuples can contain repeated values and different data types.
  • Commas create tuple structure; a singleton tuple requires a trailing comma.
  • Tuple indexes begin at 0, and negative indexes count from the end.
  • Tuples are immutable, so item assignment, insertion, removal, and reordering are not allowed.
  • A for loop can iterate through tuple items without changing them.
  • Use a list for changeable collections and a tuple for fixed collections.

For a focused reference, see What Are Tuples?.