Skip to content
Related Articles

Related Articles

Python | N element incremental tuples

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Nov, 2019

Sometimes, while working with data, we can have a problem in which we require to gather a data that is of the form of sequence of increasing element tuple with each tuple containing the element N times. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using generator expression + tuple()
The combination of above functions can be used to perform this task. In this, we need to iterate through N using generator expression and construction of tuple using tuple().




# Python3 code to demonstrate working of
# N element incremental tuples
# Using generator expression + tuple
  
# initialize N 
N = 3 
  
# printing N
print("Number of times to repeat : " + str(N))
  
# N element incremental tuples
# Using generator expression + tuple
res = tuple((ele, ) * N for ele in range(1, 6))
  
# printing result
print("Tuple sequence : " + str(res))

Output :

Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))

 

Method #2 : Using repeat() + list comprehension
This task can also be performed using combination of above functions. In this, we use repeat() to repeat elements N times. And iteration is handled using list comprehension.




# Python3 code to demonstrate working of
# N element incremental tuples
# Using generator expression + tuple
from itertools import repeat
  
# initialize N 
N = 3 
  
# printing N
print("Number of times to repeat : " + str(N))
  
# N element incremental tuples
# Using generator expression + tuple
res = tuple(tuple(repeat(ele, N)) for ele in range(1, 6))
  
# printing result
print("Tuple sequence : " + str(res))

Output :

Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!