Open In App

Python – Repeat Alternate Elements in list

Many times we have this particular use-case in which we need to repeat alternate element of list K times. The problems of making a double clone has been discussed but this problem extends to allow a flexible variable to define the number of times the element has to be repeated. Let’s discuss certain ways in which this can be performed.

Method #1 : Using list comprehension This particular task requires generally 2 loops and list comprehension can perform this particular task in one line and hence reduce the lines of codes and improving code readability. 






# Python3 code to demonstrate
# Alternate Element Repetition
# using list comprehension
 
# initializing list of lists
test_list = [4, 5, 6]
 
# printing original list
print("The original list : " + str(test_list))
 
# declaring magnitude of repetition
K = 3
 
# using list comprehension
# Alternate Element Repetition
res = [ele for idx, ele in enumerate(test_list)
       for i in range(K) if idx % 2 == 0]
 
# printing result
print("The list after alternate repeating elements : " + str(res))

Output
The original list : [4, 5, 6]
The list after alternate repeating elements : [4, 4, 4, 6, 6, 6]

Time Complexity: O(n)
Auxiliary Space: O(n)



Method #2: Using itertools.chain.from_iterable() + itertools.repeat() This particular problem can also be solved using python inbuilt functions of itertools library. The repeat function, as name suggests does the task of repetition and grouping into a list is done by the from_iterable function. 




# Python3 code to demonstrate
# Alternate Element Repetition
# using itertools.chain.from_iterable() + itertools.repeat()
import itertools
 
# initializing list of lists
test_list = [4, 5, 6]
 
# printing original list
print("The original list : " + str(test_list))
 
# declaring magnitude of repetition
K = 3
 
# using itertools.chain.from_iterable() + itertools.repeat()
# Alternate Element Repetition
res = list(itertools.chain.from_iterable(itertools.repeat(ele, K)
                                         for idx, ele in enumerate(test_list) if idx % 2 == 0))
 
# printing result
print("The list after alternate repetition elements : " + str(res))

Output
The original list : [4, 5, 6]
The list after alternate repetition elements : [4, 4, 4, 6, 6, 6]

Time complexity: O(n) as it requires iterating through the original list of size n and repeating each element K times, resulting in a total of n*K operations.
Auxiliary space: O(n*K) as it creates a new list to store the result of the repeated elements.

Method #3 : Using extend() method and * operator




# Python3 code to demonstrate
# Alternate Element Repetition
 
# initializing list of lists
test_list = [4, 5, 6]
 
# printing original list
print("The original list : " + str(test_list))
 
# declaring magnitude of repetition
K = 3
res=[]
for i in range(0,len(test_list)):
    if(i%2==0):
        res.extend([test_list[i]]*K)
         
 
# printing result
print("The list after alternate repeating elements : " + str(res))

Output
The original list : [4, 5, 6]
The list after alternate repeating elements : [4, 4, 4, 6, 6, 6]

Time complexity: O(n), where n is the length of the input list. The loop runs n times.
Auxiliary space: O(K), where K is the magnitude of repetition.

Method #4: Using slicing and extend() method 

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate
# Alternate Element Repetition
 
# initializing list of lists
test_list = [4, 5, 6]
alternate_list = test_list[::2]
# printing original list
print("The original list : " + str(test_list))
 
# declaring magnitude of repetition
K = 3
res = []
for i in range(len(alternate_list)):
    res.extend([alternate_list[i]]*K)
 
 
# printing result
print("The list after alternate repeating elements : " + str(res))

Output
The original list : [4, 5, 6]
The list after alternate repeating elements : [4, 4, 4, 6, 6, 6]

The time complexity of this code is O(N), where N is the length of the original list test_list.

 The auxiliary space used by this code is O(N), where N is the length of the original list test_list. 

Method #5 :  Here’s another approach using the numpy library:

Note: Install numpy module using command “pip install numpy”




# Python3 code to demonstrate
# Alternate Element Repetition
import numpy as np
# printing original list
test_list = [4, 5, 6]
K = 3
 
res = np.repeat(np.array(test_list[::2]), K).tolist()
# printing result
print("The list after alternate repeating elements :", res)
#This code is contributed by Edula Vinay Kumar Reddy

Output:

The list after alternate repeating elements : [4, 4, 4, 6, 6, 6]

Time Complexity: O(n), where n is the length of the input list. The time complexity is linear with the length of the list as it is just repeating and converting the elements in the list.
Auxiliary Space: O(n), where n is the length of the output list after repeating the elements. The space complexity is linear with the length of the output list as it requires memory to store the repeated elements.

Method 6: Using map()+ lambda function

Uses a map() function with a lambda function to repeat the selected elements K times, and the extend() function to append the repeated elements to a new list.

Below is the implementation of the above approach:




# Initializing list of lists
test_list = [4, 5, 6]
 
# Printing original list
print("The original list: " + str(test_list))
 
# Declaring magnitude of repetition
K = 3
 
# Using map and lambda function for alternate element repetition
res = []
for i, val in enumerate(test_list):
    if i % 2 == 0:
        res.extend(map(lambda x: val, range(K)))
 
# Printing result
print("The list after alternate repeating elements: " + str(res))

Output
The original list: [4, 5, 6]
The list after alternate repeating elements: [4, 4, 4, 6, 6, 6]

Time complexity: O(NK) where N is the length of the input list.
Auxiliary space: O(NK)

Method #7 : Using extend()+operator.mul() methods

Approach

  1. Initiate a for loop with index from 0 to length of list
  2. Find alternate element index by checking if the index is divisible by 2
  3. Access the alternate element by index and repeat element by using operator.mul() and append it to output list
  4. Display output list




# Python3 code to demonstrate
# Alternate Element Repetition
 
# initializing list of lists
test_list = [4, 5, 6]
 
# printing original list
print("The original list : " + str(test_list))
 
# declaring magnitude of repetition
K = 3
res=[]
import operator
for i in range(0,len(test_list)):
    if(i%2==0):
        res.extend(operator.mul([test_list[i]],K))
         
 
# printing result
print("The list after alternate repeating elements : " + str(res))

Output
The original list : [4, 5, 6]
The list after alternate repeating elements : [4, 4, 4, 6, 6, 6]

Time complexity: O(NK) where N is the length of the input list.
Auxiliary space: O(NK)


Article Tags :