ADVERTISEMENT
ADVERTISEMENT

Python program for creating N element incremental tuple

In this program, we are given a number N and a range (L, R). We need to create Python Program for creating N element incremental tuple.
Submitted by Shivang Yadav, on December 15, 2021

In Python, there are a lot of implementations where storing and creating repetitive values as elements of a collection is required. Learning to perform such a simple task can be useful in your programming carrier. Here, we are given a number N and a range (L, R). We need to create Python Program for creating N element incremental tuple.

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)

N element incremental Tuples

In this program, we are given a number N and a range (L, R). We need to create Python program for creating N element incremental tuple. We will create a tuple for each element of the range, with i [in the range (L, R)] repeatedly N times.

Input:
N = 4, [L, R] = [3, 6]

Output:
((3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6), )

For this, we will loop for the range and for each number of range create repeat the given number N time and create a tuple with these elements.

In Python, there are multiple methods to perform the task.

Method 1:

One method to create N incremental tuples is by using a generator expression in which we will iterate through all elements in the range [L, R] and create a tuple with N element of that range. And finally, encapsulate it in a tuple using the tuple() method.

# Python program for creating 
# N element incremental tuple

# Initializing and printing N and range
N = 4
L = 2
R = 7

print("The Value of N is " + str(N))
print("The range is (" + str(L) + ", " + str(R) + ")")

# creting N element incremental tuples
incrTuple = tuple((value, ) * N for value in range(L, R))

print("Tuple consisting of N element incremental Tuple is " + str(incrTuple))

Output:

The Value of N is 4
The range is (2, 7)
Tuple consisting of N element incremental Tuple is ((2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6))

Method 2:

Another method to solve the problem is by using the repeat method to repeat the value of range N times. And create a tuple using the generator expression only.

# Python program for creating 
# N element incremental tuple

import itertools

# Initializing and printing N and range
N = 4
L = 2
R = 7

print("The Value of N is " + str(N))
print("The range is (" + str(L) + ", " + str(R) + ")")

# creting N element incremental tuples
incrTuple = tuple(tuple(itertools.repeat(ele, N)) for ele in range(L, R))

print("Tuple consisting of N element incremental Tuple is " + str(incrTuple))

Output:

The Value of N is 4
The range is (2, 7)
Tuple consisting of N element incremental Tuple is ((2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6))

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.