Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Elements frequency in Tuple
Next article icon

Python | N element incremental tuples

Last Updated : 17 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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)).


Next Article
Python - Elements frequency in Tuple
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • Python - Elements frequency in Tuple
    Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
    7 min read
  • Python | Elementwise AND in tuples
    Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise AND operation across tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression
    5 min read
  • Python - Element Index in Range Tuples
    Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
    6 min read
  • Python - Modulo of tuple elements
    Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed.  Method #1: Using zip() + generator expression  The combination of the above
    8 min read
  • Python - Join Tuples if similar initial element
    Sometimes, while working with Python tuples, we can have a problem in which we need to perform concatenation of records from the similarity of initial element. This problem can have applications in data domains such as Data Science. Let's discuss certain ways in which this task can be performed. Inp
    8 min read
  • Find Unique Elements from Tuple in Python
    Tuples are immutable built-in data type in Python that can store multiple values in it. Extracting Unique Elements from a Tuple in Python can be done through two different approaches. Examples: Input: (1, 2, 13, 4, 3, 12, 5, 7, 7, 2, 2, 4)Output: (1, 2, 3,4,5,12,13)Input: ('Apple', 'Mango', 'Banana'
    5 min read
  • Python - Get Even indexed elements in Tuple
    Sometimes, while working with Python data, one can have a problem in which we need to perform the task of extracting just even indexed elements in tuples. This kind of problem is quite common and can have possible application in many domains such as day-day programming. Let's discuss certain ways in
    5 min read
  • Python - Elements frequency in Tuple Matrix
    Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved. Inp
    5 min read
  • Python Tuple - len() Method
    While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: [GFGTABS] Python3 Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(le
    2 min read
  • Python - Count elements in tuple list
    Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed. Method #1: Using len()
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences