How does the repetition operator work on a tuple in Python?



In Python, the "*" operator can be used to repeat a tuple multiple times. This is known as tuple repetition. It creates a new tuple with repeated elements but does not modify the original tuple. Following are some points to remember -

  • The repetition operator (*) creates a new tuple.

  • Individual items are not repeated, only the entire tuple.

  • The repeat count must be a non-negative integer.

  • The original tuple is unaltered.

  • Accepts tuples of any length and mixed data types.

How Repetition Operator Works?

When you apply '*' to a tuple with an integer, Python duplicates the tuple elements according to that integer value.

For example, if you have a tuple tup = (1, 2, 3) and you do tup * 3, the result will be (1, 2, 3, 1, 2, 3, 1, 2, 3).

Here, the tuple is repeated three times. But if you try to repeat a tuple with a negative integer or zero, the result will be an empty tuple.

Syntax

The syntax for repeating a tuple is as follows -

new_tuple = original_tuple * n

Where original_tuple is the tuple you want to repeat, and n is the number of times you want to repeat it.

Example of Tuple Repetition

Here are some examples of how to use the repetition operator on a tuple.

Example 1

In this example, we create a tuple and repeat it three times using the '*' operator.

# Create a tuple
my_tuple = (1, 2, 3)

# Repeat the tuple 3 times
repeated_tuple = my_tuple * 3

# Print the result
print(repeated_tuple)

When you run the program, it will show this output -

(1, 2, 3, 1, 2, 3, 1, 2, 3)

Example 2

In this example, we create a tuple with mixed data types and repeat it two times.

# Create a tuple with mixed data types
my_tuple = (1, "Hello", 3.14)

# Repeat the tuple 2 times 
repeated_tuple = my_tuple * 2

# Print the result
print(repeated_tuple)

After running the program, you will get this result -

(1, 'Hello', 3.14, 1, 'Hello', 3.14)

Example 3

Here we repeat a single-valued tuple. We use the comma to denote that this is a single-valued tuple.

# Create a single-valued tuple
my_tuple = (10,) * 5

# Print the result
print(my_tuple)

After running the program, you will get this result -

(10, 10, 10, 10, 10)

Example 4

In the following example code, we use the multiplication operation to form a tuple with repeated values.

# Create a tuple
my_tuple = (10, 20, 30) * 5

# Print the result
print(my_tuple)

When you run the program, it will show this output -

(10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30)

Using the repeat() Function

The repeat() is imported from the itertools module. In the repeat() function, we give the data and the number of times the data is to be repeated as arguments.

Example

In the following example, we repeat a tuple by using the repeat() function.

# Import the itertools module
import itertools

# Create a tuple
my_tuple = (10,20)

# Repeat the tuple 5 times
res = tuple(itertools.repeat(my_tuple, 5))

# Print the result
print(res)

This output will be displayed when the program runs -

((10, 20), (10, 20), (10, 20), (10, 20), (0, 20))
Updated on: 2025-06-11T10:45:32+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started