tuple
The built-in tuple data type provides an immutable sequence of values that can store any data type. Tuples are useful for storing heterogeneous data, such as records in a database or fixed collections of items:
>>> coordinates = (10.5, 20.3)
>>> person = ("Alice", 30, "Engineer")
Because tuples are immutable, they’re also hashable as long as all their items are. That lets you use a tuple as a dictionary key or store one in a set, which you can’t do with a list:
>>> locations = {(10.5, 20.3): "warehouse", (4.0, 8.1): "depot"}
>>> locations[(10.5, 20.3)]
'warehouse'
tuple Literals
In Python, you can use different literals to create tuples:
| Literal Type | Syntax Example | Description |
|---|---|---|
| Empty tuple | () |
An empty tuple, denoted simply by an empty pair of parentheses |
| Single-item tuple | (item,) or item, |
A tuple with a single element requires a trailing comma to differentiate from regular parentheses |
| Multiple-item tuple | (1, 2, 3) or 1, 2, 3 |
A standard tuple with multiple elements separated by commas |
Note: It’s the comma that makes a tuple, not the parentheses. The parentheses are optional, except for the empty tuple and when you need them to avoid syntactic ambiguity. For example, f(a, b, c) passes three arguments, while f((a, b, c)) passes a single three-item tuple. They’re also commonly used to improve the code’s readability.
tuple Constructor
tuple(iterable=(), /)
Arguments
Return Value
- Returns a Python
tupleobject
tuple Examples
Creating an empty tuple using a pair of parentheses:
>>> empty_tuple = ()
>>> empty_tuple
()
Creating tuples using literals:
>>> numbers = (1, 2, 3)
>>> numbers
(1, 2, 3)
>>> record = "Jane Doe", 25, "Canada"
>>> record
('Jane Doe', 25, 'Canada')
Creating a tuple from a list using the tuple() constructor:
>>> tuple([1, 2, 3])
(1, 2, 3)
Accessing values in a tuple through indexing:
>>> numbers[0]
1
>>> record[2]
'Canada'
Note: Tuples are immutable, so you can’t change or delete items in place.
tuple Methods
| Method | Description |
|---|---|
.count() |
Returns the number of times a specified value appears in the tuple. |
.index() |
Returns the index of the first occurrence of a specified value. |
Note: You can subscript tuple directly in type annotations since Python 3.9. Write tuple[int, str] for a fixed-shape tuple and tuple[int, ...] for a variable-length tuple whose items share one type. This syntax replaces the deprecated typing.Tuple.
tuple Common Use Cases
The most common use cases for tuple include:
- Storing fixed collections of related data
- Returning multiple values from a function
- Grouping data that shouldn’t change over time
- Serving as dictionary keys or set members
tuple Real-World Example
Imagine you’re working on a program that processes RGB color values. Tuples are perfect for this task because they hold a fixed number of elements:
>>> rgb_red = (255, 0, 0)
>>> rgb_green = (0, 255, 0)
>>> rgb_blue = (0, 0, 255)
In this example, each tuple holds an RGB color. Tuples’ immutability ensures that these colors remain constant and unchanged throughout your program.
Related Resources
Tutorial
Python's tuple Data Type: A Deep Dive With Examples
In Python, a tuple is a built-in data type that allows you to create immutable sequences of values. The values or items in a tuple can be of any type. This makes tuples pretty useful in those situations where you need to store heterogeneous data, like that in a database record, for example.
For additional information on related topics, take a look at the following resources:
- Lists vs Tuples in Python (Tutorial)
- Python's Mutable vs Immutable Types: What's the Difference? (Tutorial)
- Write Pythonic and Clean Code With namedtuple (Tutorial)
- Lists and Tuples in Python (Course)
- Exploring Python's tuple Data Type With Examples (Course)
- Writing Clean, Pythonic Code With namedtuple (Course)
- Python's tuple Data Type: A Deep Dive With Examples (Quiz)
- Lists vs Tuples in Python (Quiz)
- Differences Between Python's Mutable and Immutable Types (Course)
- Write Pythonic and Clean Code With namedtuple (Quiz)