Python – Custom element repetition
Last Updated :
05 Apr, 2023
Given list of elements and required occurrence list, perform repetition of elements.
Input : test_list1 = [“Gfg”, “Best”], test_list2 = [4, 5]
Output : [‘Gfg’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘Best’, ‘Best’, ‘Best’, ‘Best’, ‘Best’]
Explanation : Elements repeated by their occurrence number.
Input : test_list1 = [“Gfg”], test_list2 = [5]
Output : [‘Gfg’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘Gfg’]
Explanation : Elements repeated by their occurrence number.
Method #1 : Using loop + extend()
The combination of above functions provide one of the ways in which this task can be performed. In this, we iterate using loop and perform extension of elements with repetition using extend().
Step-by-step approach:
- Create an empty list called res.
- Iterate over a range of indices from 0 to the length of test_list1 using a for loop and the range() function.
- For each index idx, use the extend() method to add test_list1[idx] to the res list, repeated test_list2[idx] number of times. This is achieved by multiplying the list [test_list1[idx]] by test_list2[idx].
- Print the final result using the print() function and passing the string representation of res.
Below is the implementation of the above approach:
Python3
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for idx in range ( 0 , len (test_list1)):
res.extend([test_list1[idx]] * test_list2[idx])
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time complexity: O(n), where n is the length of the lists test_list1 and test_list2.
Auxiliary space: O(n), where n is the length of the output list res.
Method #2 : Using loop + zip()
This is yet another way in which this task can be performed. In this, we use zip() to match element with its repetition occurrence and perform required task of duplication.
Python3
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for ele, occ in zip (test_list1, test_list2):
res.extend([ele] * occ)
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time complexity: O(n),
Auxiliary space: O(k),
Method #3 : Using nested for loops
Approach:
- Used nested for loops
- First loop to access list of strings(test_list1)
- Second loop to repeat those strings based on(test_list2)
- Append all the strings to output list
- Display output list
Python3
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for i in range ( 0 , len (test_list1)):
for k in range ( 1 ,test_list2[i] + 1 ):
res.append(test_list1[i])
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time Complexity : O(N*N)
Auxiliary Space : O(N)
Method #4: Using list comprehension
Python3
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [test_list1[i] for i in range ( len (test_list1)) for k in range (test_list2[i])]
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time Complexity : O(N*N), where N is the length of given test_list
Auxiliary Space : O(N)
Method #5 : Using operator.mul()+extend() methods
- Repeat each element of test_list1 by each element of test_list2 using operator.mul()
- Append elements to output list using extend() method
- Display output list
Python3
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
import operator
for i in range ( 0 , len (test_list1)):
res.extend(operator.mul([test_list1[i]],test_list2[i]))
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time Complexity : O(N*N), where N is the length of given test_list
Auxiliary Space : O(N)
Method 6 :Using the itertools.repeat function and the zip function.
Here’s the step-by-step approach for this method:
- Import the itertools module.
- Use the itertools.repeat function to create an iterator of repeated elements of the first list.
- Use the zip function to combine the iterator from step 2 with the second list.
- Use the list comprehension to create the final list.
- Print the final list.
Python3
import itertools
test_list1 = [ "Gfg" , "is" , "Best" ]
test_list2 = [ 4 , 3 , 5 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [elem for elem, count in zip (test_list1, itertools.repeat( 1 )) for _ in range (count * test_list2[test_list1.index(elem)])]
print ( "The repeated list : " + str (res))
|
Output
The original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : [4, 3, 5]
The repeated list : ['Gfg', 'Gfg', 'Gfg', 'Gfg', 'is', 'is', 'is', 'Best', 'Best', 'Best', 'Best', 'Best']
Time complexity: O(n), where n is the length of the lists
Auxiliary space: O(n), where n is the length of the lists (used to store the final list)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...