Python Tuple Repetition with the * Operator
Learn how Python tuple repetition works with the * operator, including syntax, mutable element pitfalls, performance tradeoffs, and alternatives.

In Python, the * operator creates a new tuple by repeating the elements of an existing tuple a specified number of times. For example, (1, 2) * 3 returns (1, 2, 1, 2, 1, 2). This behavior is part of the sequence protocol and is shared with lists and strings, but tuple repetition has specific characteristics worth understanding when working with immutable data structures.
How Tuple Repetition Works
The * operator on a tuple returns a new tuple that contains the original elements repeated n times. The original tuple is not modified; instead, a new tuple object is allocated and filled with references to the same elements.
original = (1, 2, 3)
repeated = original * 2
print(repeated) # (1, 2, 3, 1, 2, 3)
print(original) # (1, 2, 3) - unchangedThe repetition factor n must be an integer. If n is zero or negative, the result is an empty tuple:
print((1, 2) * 0) # ()
print((1, 2) * -1) # ()If n is not an integer, Python raises a TypeError. This is consistent with other sequence types.
Repeating a Single Element into a Tuple
A common use case is creating a tuple with the same value repeated many times. The idiomatic way is to use a one-element tuple multiplied by n:
zeros = (0,) * 5
print(zeros) # (0, 0, 0, 0, 0)The comma after 0 is essential; (0) * 5 would just multiply the integer 0 and yield 0. The parentheses alone do not create a tuple.
For large n, tuple(itertools.repeat(value, n)) can be more readable and avoids constructing a temporary one-element tuple. However, for most practical sizes, the * operator is concise and efficient.
import itertools
zeros = tuple(itertools.repeat(0, 5))
print(zeros) # (0, 0, 0, 0, 0)Both approaches produce the same tuple. itertools.repeat returns a lazy iterator, but passing it to tuple() materializes the result into a tuple. If you need to iterate over repeated values without storing them, use the iterator directly.
Tuple Repetition with Mutable Elements
A subtle but critical detail is that tuple repetition copies references, not the objects themselves. If the tuple contains a mutable object, such as a list or a dictionary, all repeated positions refer to the same object. Modifying that object through one reference will affect every occurrence.
row = [0]
grid = (row,) * 3
print(grid) # ([0], [0], [0])
grid[0].append(1)
print(grid) # ([0, 1], [0, 1], [0, 1])This behavior is identical to list repetition and is often the source of bugs. If you need independent copies of a mutable element, you must create them explicitly, for example with a tuple comprehension:
grid = tuple([0] for _ in range(3))
grid[0].append(1)
print(grid) # ([0, 1], [0], [0])The same principle applies to any mutable object, including sets and custom class instances.
Performance and Memory Considerations
Tuple repetition is a shallow operation: it allocates a new tuple and copies references from the original. The time and additional memory cost are proportional to the length of the resulting tuple, which is len(original) * n. For small tuples and reasonable n, this is negligible. For very large repetitions, the resulting tuple can consume significant memory because it holds many references, even though the referenced objects themselves are not duplicated.
If you need to iterate over repeated values without storing them all at once, consider using itertools.repeat in a loop or a generator expression. For example:
for value in itertools.repeat(1, 1000000):
process(value)This avoids creating a tuple with a million elements. However, if you specifically need a tuple object, repetition is the direct way to create it.
Common Mistakes and Edge Cases
One frequent mistake is forgetting the comma when creating a one-element tuple for repetition. (0) * 5 multiplies the integer 0 and returns 0, not a tuple. Always write (0,) * 5.
Another edge case is using a floating-point repetition count. Python will raise TypeError: can't multiply sequence by non-int of type 'float'. This is a safety measure to avoid ambiguous behavior.
Repetition also interacts with concatenation. The * operator has higher precedence than + for sequences, so (1, 2) + (3,) * 2 evaluates as (1, 2) + ((3,) * 2), resulting in (1, 2, 3, 3). If you intend to repeat the concatenated result, use parentheses: ((1, 2) + (3,)) * 2 yields (1, 2, 3, 1, 2, 3).
Finally, remember that repetition of an empty tuple always yields an empty tuple, regardless of n. This is consistent with sequence semantics.
Alternatives to Tuple Repetition
While * is the most direct way to repeat a tuple, there are alternatives depending on the situation.
tuple(itertools.repeat(x, n))creates a tuple ofncopies of a single value. It is more explicit and avoids the one-element tuple syntax.- A generator expression with
rangecan produce a tuple with varying values:tuple(i for i in range(5))is not repetition, but it can be used to build a tuple from a pattern. - For repeating a tuple's contents multiple times in a larger sequence, you might use
tuple(itertools.chain.from_iterable(itertools.repeat(t, n))), but that is rarely necessary.
The * operator remains the most readable and efficient choice for straightforward repetition.
When to Use Tuple Repetition in Practice
Tuple repetition is useful when you need a fixed-size tuple with repeated elements, such as initializing a default row, creating a constant vector, or generating a mask. It is also common in data structures where immutability is required, like using a tuple as a dictionary key.
Avoid tuple repetition when the elements are mutable and you need independent instances. In that case, use a comprehension or a loop to create separate objects. Also avoid it for extremely large repetitions where a lazy iterator would be more memory-efficient.
Use * when you need a concrete tuple and the repetition count is moderate. For lazy iteration over repeated values, prefer itertools.repeat. For mutable elements, always construct fresh objects explicitly. These choices will keep your code correct and efficient.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.