Open In App

Python – Custom element repetition

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Custom elements repetition
# Using  loop + extend()
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using loop to perform iteration
res = []
for idx in range(0, len(test_list1)):
     
    # using extend to perform element repetition
    res.extend([test_list1[idx]] * test_list2[idx])
     
# printing result
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




# Python3 code to demonstrate working of
# Custom elements repetition
# Using  loop + zip()
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using zip() to intervene elements and occurrence
res = []
for ele, occ in zip(test_list1, test_list2):
    res.extend([ele] * occ)
     
# printing result
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:

  1. Used nested for loops
  2. First loop to access list of strings(test_list1)
  3. Second loop to repeat those strings based on(test_list2)
  4. Append all the strings to output list
  5. Display output list

Python3




# Python3 code to demonstrate working of
# Custom elements repetition
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using loop to perform iteration
res = []
for i in range(0, len(test_list1)):
    for k in range(1,test_list2[i]+1):
        res.append(test_list1[i])
     
     
# printing result
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




# Python3 code to demonstrate working of
# Custom elements repetition
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using list comprehension to perform iteration
res = [test_list1[i] for i in range(len(test_list1)) for k in range(test_list2[i])]
 
# printing result
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

  1. Repeat each element of test_list1 by each element of test_list2 using operator.mul()
  2. Append elements to output list using extend() method
  3. Display output list

Python3




# Python3 code to demonstrate working of
# Custom elements repetition
# Using loop + extend()
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using loop to perform iteration
res = []
import operator
for i in range(0, len(test_list1)):
    res.extend(operator.mul([test_list1[i]],test_list2[i]))
# printing result
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:

  1. Import the itertools module.
  2. Use the itertools.repeat function to create an iterator of repeated elements of the first list.
  3. Use the zip function to combine the iterator from step 2 with the second list.
  4. Use the list comprehension to create the final list.
  5. Print the final list.

Python3




# Python3 code to demonstrate working of
# Custom elements repetition
# Using itertools.repeat() and zip()
 
# import the itertools module
import itertools
 
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = [4, 3, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using itertools.repeat() and zip() to create the repeated list
res = [elem for elem, count in zip(test_list1, itertools.repeat(1)) for _ in range(count*test_list2[test_list1.index(elem)])]
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads