Complete guide to Python's tuple function covering creation, conversion, and practical examples of immutable sequences.
Last modified April 11, 2025
This comprehensive guide explores Python’s tuple function, which creates immutable sequence objects. We’ll cover creation, conversion from other iterables, and practical examples of using tuples in Python programs.
The tuple function creates a new tuple object. It can convert other iterables to tuples or create empty tuples. Tuples are immutable, ordered collections that can contain any Python objects.
Key characteristics: immutable sequence, ordered elements, can contain heterogeneous types, supports indexing and slicing, and is hashable if all elements are hashable.
Here’s basic usage showing how to create empty tuples and tuples with one element (note the comma requirement for single-element tuples).
basic_tuple.py
empty = tuple() print(empty) # () print(type(empty)) # <class ’tuple’>
single = tuple([42]) # Using iterable single2 = (42,) # Using literal print(single) # (42,) print(single2) # (42,)
This example shows two ways to create empty tuples and the important comma requirement for single-element tuples. Without the comma, Python interprets parentheses as grouping operators.
The tuple() call without arguments creates an empty tuple, same as the literal (). For single elements, the comma makes it a tuple.
The tuple function can convert any iterable to a tuple. This example demonstrates conversion from lists, strings, and dictionaries.
conversion.py
numbers = [1, 2, 3] tuple_numbers = tuple(numbers) print(tuple_numbers) # (1, 2, 3)
text = “hello” tuple_chars = tuple(text) print(tuple_chars) # (‘h’, ’e’, ’l’, ’l’, ‘o’)
person = {’name’: ‘Alice’, ‘age’: 25} tuple_keys = tuple(person) print(tuple_keys) # (’name’, ‘age’)
This shows how tuple can convert various iterables. Lists become tuples with the same elements. Strings become tuples of characters.
Dictionaries convert to tuples of their keys by default. To get key-value pairs, use person.items() as the iterable.
Tuples are often used for unpacking multiple values and handling variable function arguments. This example demonstrates both patterns.
unpacking.py
coordinates = (10.5, 20.3) x, y = coordinates print(f"x: {x}, y: {y}") # x: 10.5, y: 20.3
def print_args(*args): print(tuple(args))
print_args(1, 2, 3) # (1, 2, 3)
Tuple unpacking allows assigning multiple variables at once from a tuple. The *args syntax collects positional arguments into a tuple.
These patterns are common in Python for clean multi-value handling and creating flexible function interfaces.
This example demonstrates tuple immutability by showing what operations are allowed and which raise exceptions.
immutability.py
colors = tuple([‘red’, ‘green’, ‘blue’])
print(colors[1]) # ‘green’ (indexing) print(colors[:2]) # (‘red’, ‘green’) (slicing) print(len(colors)) # 3
try: colors[1] = ‘yellow’ # Attempt modification except TypeError as e: print(f"Error: {e}") # ’tuple’ object does not support item assignment
Tuples support all sequence operations that don’t modify them, like indexing, slicing, and length checks. Attempting to modify elements raises TypeError.
This immutability makes tuples useful as dictionary keys (when all elements are hashable) and for storing constant data that shouldn’t change.
This example compares tuple and list performance for creation and iteration, showing tuples’ advantages in certain scenarios.
performance.py
import timeit
list_time = timeit.timeit(‘x = [1, 2, 3, 4, 5]’, number=1000000) tuple_time = timeit.timeit(‘x = (1, 2, 3, 4, 5)’, number=1000000)
print(f"List creation: {list_time:.3f}") print(f"Tuple creation: {tuple_time:.3f}")
list_iter = timeit.timeit(‘for i in x: pass’, ‘x = [1, 2, 3, 4, 5]’, number=1000000) tuple_iter = timeit.timeit(‘for i in x: pass’, ‘x = (1, 2, 3, 4, 5)’, number=1000000)
print(f"List iteration: {list_iter:.3f}") print(f"Tuple iteration: {tuple_iter:.3f}")
Tuples generally create faster than lists because of their immutability. Iteration speeds are similar, but tuples have a memory advantage for fixed collections.
Use tuples for heterogeneous, unchanging data and lists for homogeneous, modifiable collections.
Use for fixed data: Prefer tuples for collections that won’t change
As dictionary keys: Use tuples (with hashable elements) as keys
Function returns: Return multiple values as tuples
Memory efficiency: Use tuples for large constant sequences
Document intent: Use tuples to signal immutable data
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.