Python | List expansion by K
Last Updated :
02 May, 2023
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
test_list = [ 4 , 5 , 2 , 8 ]
print ( "The original list : " + str (test_list))
K = 3
res = [i for i in test_list for j in range (K)]
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
from itertools import chain, tee
test_list = [ 4 , 5 , 2 , 8 ]
print ( "The original list : " + str (test_list))
K = 3
res = list (chain( * zip ( * tee(test_list, K))))
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
test_list = [ 4 , 5 , 2 , 8 ]
print ( "The original list : " + str (test_list))
K = 3
res = []
for i in test_list:
res.extend([i] * K)
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:
Python3
import itertools
def expand_list(input_list, k):
expanded_list = []
for element in input_list:
expanded_list.extend(itertools.repeat(element, k))
return expanded_list
test_list = [ 4 , 5 , 2 , 8 ]
k = 3
expanded_list = expand_list(test_list, k)
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(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:
- Initialize an empty list res to store the extended list.
- Iterate through each element i in the original list test_list using a for loop.
- For each i, iterate from 0 to K-1 using another for loop.
- In the inner for loop, append the current value of i to the res list K times using the append() method and the value of j.
- Once all elements in test_list have been processed, the res list contains the extended list.
- Print the extended list using print()
Below is the implementation of the above approach:
Python3
test_list = [ 4 , 5 , 2 , 8 ]
print ( "The original list : " + str (test_list))
K = 3
res = []
for i in test_list:
for j in range (K):
res.append(i)
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.
Python3
import numpy as np
def expand_list(input_list, k):
arr = np.array(input_list)
expanded_arr = np.repeat(arr, k)
expanded_list = expanded_arr.tolist()
return expanded_list
test_list = [ 4 , 5 , 2 , 8 ]
k = 3
expanded_list = expand_list(test_list, k)
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.
Share your thoughts in the comments
Please Login to comment...