# Python program to repeat tuples N times

In this problem, we are given a tuple and an integer value N. Our task is to create a Python program to repeat tuples N times.
Submitted by Shivang Yadav, on January 13, 2022

While working with Python programming language, creating duplicate values of the given collection is important. Python provides tools to perform such tasks. Here, we will see a Python program to repeat tuples N times.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

```tuple = ("python", "includehelp", 43, 54.23)
```

## Repeating tuples N times

We are given a tuple with integer values and an integer N. We need to create a Python program to create a new tuple that will contain the given tuple repeated N times.

```Input:
tup1 = (2, 9), N = 5

Output:
((2, 9) ,(2, 9) ,(2, 9) ,(2, 9) ,(2, 9))
```

To create duplicate tuples N times, we need to create an empty tuple and concatenate the values of the tuple to it N times. Python provides some methods that can help to ease the programmer's work performing these tasks, let's learn them here.

Method 1:

One method to perform the task is by using the multiplication operator "*" which will multiply the tuples and create a new tuple with the values the tuple repeated N times.

```# Python program to repeat tuples N times

# Initializing and printing tuple
myTup = (32, 8)
print("The elements of the given tuple are " + str(myTup))
N = 4

# Repeating Tuple N times
repeatTuple = ((myTup, ) * N)

# printing result
print("The tuple repeated N times is : " + str(repeatTuple))
```

Output:

```The elements of the given tuple are (32, 8)
The tuple repeated N times is : ((32, 8), (32, 8), (32, 8), (32, 8))
```

Method 2:

Another method to solve the problem is by using the repeat() method present in Python's itertools library. This will return a list consisting of the tuple repeated N times. Then we will convert it into a tuple using the tuple() method.

```# Python program to repeat tuples N times

import itertools

# Initializing and printing tuple
myTup = (32, 8)
print("The elements of the given tuple are " + str(myTup))
N = 4

# Repeating Tuple N times
repeatTuple = tuple(itertools.repeat(myTup, N))

# Printing result
print("The tuple repeated N times is " + str(repeatTuple))
```

Output:

```The elements of the given tuple are (32, 8)
The tuple repeated N times is ((32, 8), (32, 8), (32, 8), (32, 8))
```

Preparation

What's New

Top Interview Coding Problems/Challenges!