Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Repeating tuples N times

Improve Article
Save Article
  • Last Updated : 19 Jan, 2023
Improve Article
Save Article

Sometimes, while working with data, we might have a problem in which we need to replicate, i.e construct duplicates of tuples. This is an important application in many domains of computer programming. Let’s discuss certain ways in which this task can be performed. 
Method #1 : Using * operator The multiplication operator can be used to construct the duplicates of a container. This also can be extended to tuples even though tuples are immutable. 

Python3




# Python3 code to demonstrate working of
# Repeating tuples N times
# using * operator
 
# initialize tuple
test_tup = (1, 3)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize N
N = 4
 
# Repeating tuples N times
# using * operator
res = ((test_tup, ) * N)
 
# printing result
print("The duplicated tuple elements are : " + str(res))

Output

The original tuple : (1, 3)
The duplicated tuple elements are : ((1, 3), (1, 3), (1, 3), (1, 3))

  Method #2 : Using repeat() The internal function of itertools library, repeat() can be used to achieve the solution to the above problem. 

Python3




# Python3 code to demonstrate working of
# Repeating tuples N times
# using repeat()
from itertools import repeat
 
# initialize tuple
test_tup = (1, 3)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize N
N = 4
 
# Repeating tuples N times
# using repeat()
res = tuple(repeat(test_tup, N))
 
# printing result
print("The duplicated tuple elements are : " + str(res))

Output

The original tuple : (1, 3)
The duplicated tuple elements are : ((1, 3), (1, 3), (1, 3), (1, 3))

Method #3 : Using tuple() method and for loop

Python3




# Python3 code to demonstrate working of
# Repeating tuples N times
 
# initialize tuple
test_tup = (1, 3)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize N
N = 4
 
# Repeating tuples N times
res=[]
for i in range(0,N):
    res.append(test_tup)
res=tuple(res)
# printing result
print("The duplicated tuple elements are : " + str(res))

Output

The original tuple : (1, 3)
The duplicated tuple elements are : ((1, 3), (1, 3), (1, 3), (1, 3))

4. Using list comprehension:
Python3 code to demonstrate working of Repeating tuples N times using list comprehension

Python3




# Python3 code to demonstrate working of
# Repeating tuples N times
  
# initialize tuple
test_tup = (1, 3)
  
# printing original tuple
print("The original tuple : " + str(test_tup))
  
# initialize N
N = 4
  
# Repeating tuples N times
res = [test_tup for i in range(N)]
res = tuple(res)
# printing result
print("The duplicated tuple elements are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original tuple : (1, 3)
The duplicated tuple elements are : ((1, 3), (1, 3), (1, 3), (1, 3))

Time complexity: O(n) 
Auxiliary space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!