Sometime, while working with data, we can have a application in which we need to duplicate tuple elements by the amount of element count. This is very unique application but can occur in certain cases. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using nested loops
This is the brute force method by which this task can be performed. In this, the outer loop is for iteration to each element in list and inner loop is to add the similar element equating to length of respective tuple by outer loop.
# Python3 code to demonstrate working of # Extend tuples by count in list # using nested loop # initialize list of tuple test_list = [('1', '4', '6'), ('5', '8'), ('2', '9'), ('1', )] # printing original tuples list print("The original list : " + str(test_list)) # Extend tuples by count in list # using nested loop res = [] for sub in range(len(test_list)): for ele in range(len(test_list[sub])): res.append(test_list[sub]) # printing result print("The modified and extended list is : " + str(res)) |
The original list : [(‘1’, ‘4’, ‘6’), (‘5’, ‘8’), (‘2’, ‘9’), (‘1’, )]
The modified and extended list is : [(‘1’, ‘4’, ‘6’), (‘1’, ‘4’, ‘6’), (‘1’, ‘4’, ‘6’), (‘5’, ‘8’), (‘5’, ‘8’), (‘2’, ‘9’), (‘2’, ‘9’), (‘1’, )]
Method #2 : Using loop + chain()
This is yet another way in which this task can be performed. In this, we reduce one loop, inner loop and multiply the tuples into one and flatten using chain(). It may have certain overheads depending upon different cases.
# Python3 code to demonstrate working of # Extend tuples by count in list # using loop + chain() from itertools import chain # initialize list of tuple test_list = [('1', '4', '6'), ('5', '8'), ('2', '9'), ('1', )] # printing original tuples list print("The original list : " + str(test_list)) # Extend tuples by count in list # using loop + chain() res = [] for sub in range(len(test_list)): res.append([test_list[sub]]*len(test_list[sub])) res1 = chain(*res) res = list(res1) # printing result print("The modified and extended list is : " + str(res)) |
The original list : [(‘1’, ‘4’, ‘6’), (‘5’, ‘8’), (‘2’, ‘9’), (‘1’, )]
The modified and extended list is : [(‘1’, ‘4’, ‘6’), (‘1’, ‘4’, ‘6’), (‘1’, ‘4’, ‘6’), (‘5’, ‘8’), (‘5’, ‘8’), (‘2’, ‘9’), (‘2’, ‘9’), (‘1’, )]
Recommended Posts:
- Python - Extend consecutive tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Swap tuple elements in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python | How to Concatenate tuples to nested tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python - Raise elements of tuple as power to another tuple
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Check if element is present in tuple of tuples
- Python | Remove tuple from list of tuples if not containing any character
- Python | Group by matching second tuple value in list of tuples
- Python - Combinations of sum with tuples in tuple list
- Python | Reverse each tuple in a list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

