ZetCode

Python tuple Function

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.

Basic Definitions

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.

Creating Empty and Single-Element Tuples

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
empty = tuple()
print(empty)        # ()
print(type(empty))  # <class 'tuple'>

# Single-element tuple (note comma)
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.

Converting Other Iterables to Tuples

The tuple function can convert any iterable to a tuple. This example demonstrates conversion from lists, strings, and dictionaries.

conversion.py
# From list
numbers = [1, 2, 3]
tuple_numbers = tuple(numbers)
print(tuple_numbers)  # (1, 2, 3)

# From string
text = "hello"
tuple_chars = tuple(text)
print(tuple_chars)   # ('h', 'e', 'l', 'l', 'o')

# From dictionary (gets keys)
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.

Tuple Unpacking and Function Arguments

Tuples are often used for unpacking multiple values and handling variable function arguments. This example demonstrates both patterns.

unpacking.py
# Tuple unpacking
coordinates = (10.5, 20.3)
x, y = coordinates
print(f"x: {x}, y: {y}")  # x: 10.5, y: 20.3

# Variable arguments
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.

Immutable Characteristics

This example demonstrates tuple immutability by showing what operations are allowed and which raise exceptions.

immutability.py
colors = tuple(['red', 'green', 'blue'])

# Allowed operations
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.

Performance Comparison with Lists

This example compares tuple and list performance for creation and iteration, showing tuples' advantages in certain scenarios.

performance.py
import timeit

# Creation time
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}")

# Iteration time
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.

Best Practices

Source References

Author

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.