Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Extend tuples by count of elements in tuple

Improve Article
Save Article
  • Last Updated : 19 Jan, 2023
Improve Article
Save Article

Sometimes, while working with data, we can have an 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




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

Output

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




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

Output

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 #3 : Using * operator and extend() method

Python3




# Python3 code to demonstrate working of
# Extend tuples by count in list
 
# 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 i in test_list:
    a = [i]*len(i)
    res.extend(a)
# printing result
print("The modified and extended list is : " + str(res))

Output

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 #4 : Using list comprehension and * operator

Python3




# Python3 code to demonstrate working of
# Extend tuples by count in list
# using list comprehension and * operator
 
# 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 list comprehension and * operator
res = [i for i in test_list for _ in range(len(i))]
 
# printing result
print("The modified and extended list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

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',)]

This method uses list comprehension to iterate through the original list and the * operator to repeat the tuple the number of times as the number of elements in the tuple. This method is more concise and more efficient than the previous methods as it only requires one pass over the original list and avoids unnecessary appending and extending. The time complexity is O(n) and Auxiliary space is O(n) where n is the number of elements in the original list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!