N element incremental tuples in Python


When it is required to create 'N' element incremental tuples, generator expression and 'tuple' method can be used.

Below is a demonstration of the same −

Example

Live Demo

N = 3
print("The value of 'N' has been initialized")
print("The number of times it has to be repeated is : ")
print(N)

my_result = tuple((elem, ) * N for elem in range(1, 6))
print("The tuple sequence is : ")
print(my_result)

Output

The value of 'N' has been initialized
The number of times it has to be repeated is :
3
The tuple sequence is :
((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))

Explanation

  • The value of 'N' is initialized, and is displayed on the console.
  • A tuple with a specific element, multiplied 'N' times in a given range is assigned to a variable.
  • This sequence is generated, and is displayed as output on the console.

Updated on: 11-Mar-2021

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements