Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Examples of Tuple Repetition
Here are some examples of how to use the repetition operator on a tuple.
Basic Repetition
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)
Mixed Data Types
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)
Single Element Tuple
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,) # Repeat it 5 times repeated_tuple = my_tuple * 5 # Print the result print(repeated_tuple)
After running the program, you will get this result ?
(10, 10, 10, 10, 10)
Zero and Negative Repetition
When you multiply a tuple by zero or a negative number, you get an empty tuple ?
# Create a tuple
my_tuple = (1, 2, 3)
# Repeat with zero
zero_repeat = my_tuple * 0
print("Zero repetition:", zero_repeat)
# Repeat with negative number
negative_repeat = my_tuple * -2
print("Negative repetition:", negative_repeat)
Zero repetition: () Negative repetition: ()
Using the repeat() Function
The repeat() function from the itertools module works differently. It repeats the entire tuple as an object, not its contents ?
# Import the itertools module import itertools # Create a tuple my_tuple = (10, 20) # Repeat the tuple 3 times as whole objects result = tuple(itertools.repeat(my_tuple, 3)) # Print the result print(result)
This output will be displayed when the program runs ?
((10, 20), (10, 20), (10, 20))
Comparison
| Method | Result Type | Description |
|---|---|---|
tuple * n |
Flat tuple | Repeats tuple contents |
itertools.repeat() |
Nested tuple | Repeats tuple as objects |
Conclusion
The repetition operator (*) creates a new tuple by repeating the original tuple's contents multiple times. Use tuple * n for flat repetition or itertools.repeat() when you need the tuple repeated as separate objects.
