Open In App

Python | List expansion by K

Sometimes, we require to reduce the length of the python list but other times we might also require to increase its size, and that too by repeating every element N times. This type of utility can come in day-day programming. Let’s discuss certain ways in which this can be achieved. 

Method # : Using list comprehension This task can be performed using the list comprehension method which is just a shortened version of the generic loop method in which we repeat each element for K times using the iteration. 






# Python3 code to demonstrate
# List extension by K
# using list comprehension
 
# initializing list
test_list = [4, 5, 2, 8]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 3
 
# using list comprehension
# to extend list
res = [i for i in test_list for j in range(K)]
 
# printing result
print("The resultant list after extension is : " + str(res))

Output
The original list : [4, 5, 2, 8]
The resultant list after extension is : [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

Time Complexity: O(nk) where n is the length of the original list and k is the number of times to extend the list.
Auxiliary Space: O(nk) as a new list of length nk is created as the result.



Method #2 : Using itertools.chain() + itertools.tee() + zip() The combination of the above three functions can also help to achieve a solution to this particular problem. The tee function repeats in the list K times, nested unzipping which links the iteration with a particular element and the chain function performs this task for all the elements. 




# Python3 code to demonstrate
# List extension by K
# using itertools.chain() + itertools.tee() + zip()
from itertools import chain, tee
 
# initializing list
test_list = [4, 5, 2, 8]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 3
 
# using itertools.chain() + itertools.tee() + zip()
# to extend list
res = list(chain(*zip(*tee(test_list, K))))
 
# printing result
print("The resultant list after extension is : " + str(res))

Output
The original list : [4, 5, 2, 8]
The resultant list after extension is : [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

Time complexity: O(nk)
Auxiliary space: O(nk)

Method #3 : Using * and extend() method




# Python3 code to demonstrate
# List extension by K
 
# initializing list
test_list = [4, 5, 2, 8]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 3
res = []
for i in test_list:
    res.extend([i]*K)
 
# printing result
print("The resultant list after extension is : " + str(res))

Output
The original list : [4, 5, 2, 8]
The resultant list after extension is : [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

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

Method #4 : Using repeat

Here is an approach using the repeat method from the itertools module:




import itertools
 
def expand_list(input_list, k):
# Initialize an empty list to store the expanded elements
  expanded_list = []
# Iterate through each element in the input list
  for element in input_list:
# Use the repeat function from the itertools module to repeat the element k times and append it to the expanded list
    expanded_list.extend(itertools.repeat(element, k))
# Return the expanded list
  return expanded_list
 
#Initialize a list and a value for k
test_list = [4, 5, 2, 8]
k = 3
 
#Call the expand_list function to get the expanded list
expanded_list = expand_list(test_list, k)
 
#Print the original and expanded lists
print("Original list:", test_list)
print("Expanded list:", expanded_list)
#This code is contributed by Edula Vinay Kumar Reddy

Output
Original list: [4, 5, 2, 8]
Expanded list: [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

Time complexity: O(nk), where n is the length of lst and k is the number of times to repeat each element.
Auxiliary space: O(nk)

Method #5: Using a loop

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate
# List extension by K
# using a loop
 
# initializing list
test_list = [4, 5, 2, 8]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 3
 
# using a loop to extend list
res = []
for i in test_list:
    for j in range(K):
        res.append(i)
 
# printing result
print("The resultant list after extension is : " + str(res))

Output
The original list : [4, 5, 2, 8]
The resultant list after extension is : [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

Time complexity: O(nK) where n is the length of the original list and K is the extension. 
Auxiliary space: O(nK) since we are storing the extended list in a new list.

Method #6: Using numpy.repeat

Here is an approach using the numpy module’s repeat function:

Algorithm:

Import the numpy module.
Define a function expand_list that takes two arguments, input_list and k.
Convert the input_list to a numpy array using the numpy.array() method.
Use the numpy.repeat() function to repeat each element of the array k times and assign it to the variable expanded_array.
Convert the expanded_array to a list using the tolist() method and assign it to the variable expanded_list.
Return the expanded_list.
Call the expand_list function with the test_list and k values.
Print the original and expanded lists.




import numpy as np
 
def expand_list(input_list, k):
    # Convert input_list to a numpy array
    arr = np.array(input_list)
     
    # Repeat each element k times
    expanded_arr = np.repeat(arr, k)
     
    # Convert expanded array to a list
    expanded_list = expanded_arr.tolist()
     
    return expanded_list
 
# Test the function
test_list = [4, 5, 2, 8]
k = 3
expanded_list = expand_list(test_list, k)
 
# Print the original and expanded lists
print("Original list:", test_list)
print("Expanded list:", expanded_list)

Output:

Original list: [4, 5, 2, 8]
Expanded list: [4, 4, 4, 5, 5, 5, 2, 2, 2, 8, 8, 8]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(nk), where n is the length of the input list and k is the number of times to repeat each element.


Article Tags :