Python | Repeat and Multiply list extension
Sometimes, while working with Python list, we can have a problem in which we need to extend a list in a very customized way. We may have to repeat contents of list and while doing that, each time new list must be a multiple of original list. This incremental expansion has applications in many domains. Let’s discuss a way in which this task can be performed.
Method 1: Using list comprehension This task can be performed in a brute manner, but having a shorter implementation using list comprehension always is better. In this, we perform task in 2 steps, first we make a helper list to form a multiplication factor list and then cumulate the result using original list.
Python3
test_list = [ 4 , 5 , 7 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
temp = [ 1 * M * * i for i in range (N)]
res = list ([ele * tele for tele in temp for ele in test_list])
print ( "List after extension and multiplication : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and multiplication : [4, 5, 6, 12, 15, 18, 36, 45, 54, 108, 135, 162]
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Method 2: Using while loop+ extend() method
Python3
test_list = [ 4 , 5 , 7 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
j = 1
res = []
res.extend(test_list)
while (j < N):
p = []
for i in test_list:
a = i * M
p.append(a)
test_list = p
res.extend(p)
j + = 1
print ( "List after extension and multiplication : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and multiplication : [4, 5, 6, 12, 15, 18, 36, 45, 54, 108, 135, 162]
Time Complexity: O(N * length of test_list)
Auxiliary Space: O(N * length of test_list)
Method #3: Using recursion
This function takes a list, N and M as input and returns the list after extension and multiplication using recursion. it defines a recursive function repeat_multiply_list() which takes the original list, N and M as input and repeatedly calls itself to generate the extended list. In each recursive call, it first multiplies each element in the original list by M and concatenates the multiplied list with the original list and repeats the process for N-1 times. The base case is when N is 0, it returns the original list. The time complexity of this function is O(n) where n is the number of times the list needs to be extended and multiplied and the space complexity is O(n) where n is the number of elements in the extended list.
Python3
def repeat_multiply_list(original_list, N, M):
# Base case: if N is 1, return the original list
if N==1:
return original_list
else:
# Multiply each element in the original list by M
multiplied_list = [x*M for x in original_list]
# Concatenate the multiplied list with the original list
return original_list + repeat_multiply_list(multiplied_list, N-1, M)
# initializing list
test_list = [4, 5, 6]
# printing original list
print(“The original list is : ” + str(test_list))
# Extension factor
N = 4
# Multiply factor
M = 3
res = repeat_multiply_list(test_list, N, M)
# printing result
print(“List after extension and multiplication : ” + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output
The original list is : [4, 5, 6]
List after extension and multiplication : [4, 5, 6, 12, 15, 18, 36, 45, 54, 108, 135, 162]
Time complexity: O(N^2) where N is the number of times the list is repeated and multiplied.
Auxiliary space: O(N) where N is the size of the final list after all the repetitions and multiplications.
Method 4: Using map() and lambda function
In this method, we use the map() function and a lambda function to generate the temporary list of repeated powers of M. Then we use another lambda function with map() to create a nested list of multiplied values. Finally, we use list comprehension to flatten the nested list and get the final result.
- We first initialize the list test_list with some values.
- We then print the original list using the print() function.
- We define two variables, N and M, which represent the extension factor and multiply factor respectively.
- We use the map() function along with a lambda function to create a list temp that contains powers of M from 0 to N-1. This is done by applying the lambda function to each element in the range of N, which generates the powers of M.
- We use the map() function along with another lambda function to create a nested list res that contains the multiplied values. This is done by iterating over each element in temp and multiplying it with each element in test_list.
- We flatten the nested list res using a list comprehension that iterates over each element in the nested list and adds it to the final result list res.
- Finally, we print the result using the print() function.
Python3
test_list = [ 4 , 5 , 7 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
temp = list ( map ( lambda x: M * * x, range (N)))
res = list ( map ( lambda x: [i * x for i in test_list], temp))
res = [ele for sublist in res for ele in sublist]
print ( "List after extension and multiplication : " + str (res))
|
Output
The original list is : [4, 5, 7]
List after extension and multiplication : [4, 5, 7, 12, 15, 21, 36, 45, 63, 108, 135, 189]
The time complexity of the program is O(N*M), where N is the extension factor and M is the multiply factor.
The space complexity of the program is O(N*M), because we store the temporary list of powers of M and nested list of multiplied values in memory.
Method 5: Using itertools.product()
Last Updated :
17 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...