Open In App

Python | N element incremental tuples

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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))

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

Python3




# 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
res=[]
for i in range(1,6):
    x=[]
    j=0
    while(j<N):
        x.append(i)
        j+=1
    res.append(tuple(x))
res=tuple(res)
# 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 #4 : Using * operator and tuple() method

Python3




# Python3 code to demonstrate working of
# N element incremental tuples
 
# initialize N
N = 3
 
# printing N
print("Number of times to repeat : " + str(N))
 
# N element incremental tuples
res=[]
for i in range(1,6):
    a=[i]*N
    res.append(tuple(a))
res=tuple(res)
# 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))

Time complexity: O(n) where n is the number of elements in the range from 1 to 6, as the loop iterates n times and each iteration takes constant time.

Auxiliary space: O(n) to store the list of tuples where n is the number of tuples in the list.

Method 5: Using numpy library

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initialize N
N = 3
 
# printing N
print("Number of times to repeat : " + str(N))
 
# N element incremental tuples using numpy
res = [tuple(np.full(N, i)) for i 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)]

Time Complexity: O(n^2), where n is the number of elements in the result. 
Auxiliary Space: O(n^2)

Method 6: using itertools module

 Step-by-step algorithm for implementing the approach

  1. Initialize the variable N to the given value.
  2. Define the range of numbers to choose from, using the range() function.
  3. Use the product() function from the itertools library to generate all possible tuples of length N whose elements are chosen from the numbers iterable.
  4. Filter the resulting list of tuples to only those where all elements are the same, using a list comprehension and the all() function.
  5. Print the value of N to the console.
  6. Print the resulting list of tuples to the console.
  7. Return the list of tuples.

Python3




from itertools import product
 
# Set the number of times to repeat each element
N = 3
 
# Define the range of numbers to choose from
numbers = range(1, 6)
 
# Generate all possible tuples of length N whose elements are chosen from the numbers iterable
res = list(product(numbers, repeat=N))
 
# Filter the tuples to only those where all elements are the same
res = [t for t in res if all(x == t[0] for x in t)]
 
# Print the number of times to repeat each element
print("Number of times to repeat : " + str(N))
 
# Print the resulting tuple sequence
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)]

Time complexity:

  1. The product() function generates all possible tuples of length N whose elements are chosen from the numbers iterable. The number of possible tuples is len(numbers)^N.
  2. The filter operation uses a list comprehension and the all() function to filter the tuples where all elements are the same. This operation takes O(N) time for each tuple.
  3. Therefore, the overall time complexity of the algorithm is O(N*len(numbers)^N).

Auxiliary space:

  1. The product() function uses O(N*len(numbers)) auxiliary space to generate all possible tuples.
  2. The list comprehension used to filter the tuples uses O(N) auxiliary space for each tuple.
  3. Therefore, the overall auxiliary space complexity of the algorithm is O(N*len(numbers)).


Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads