Open In App

Python | Repeat and Multiply list extension

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Repeat and Multiply list extension
# Using list comprehension
 
# initializing list
test_list = [4, 5, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Multiply factor
M = 3
 
# Repeat and Multiply list extension
# Using list comprehension
temp = [1 * M**i for i in range(N)]
res = list([ele * tele for tele in temp for ele in test_list])
 
# printing result
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




# Python3 code to demonstrate working of
# Repeat and Multiply list extension
 
# initializing list
test_list = [4, 5, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Multiply factor
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
# printing result
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.

  1. We first initialize the list test_list with some values.
  2. We then print the original list using the print() function.
  3. We define two variables, N and M, which represent the extension factor and multiply factor respectively.
  4. 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.
  5. 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.
  6. 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.
  7. Finally, we print the result using the print() function.

Python3




# Python3 code to demonstrate working of
# Repeat and Multiply list extension
# Using map() and lambda function
 
# initializing list
test_list = [4, 5, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Multiply factor
M = 3
 
# Repeat and Multiply list extension
# Using map() and lambda function
temp = list(map(lambda x: M**x, range(N)))
res = list(map(lambda x: [i * x for i in test_list], temp))
 
# flatten the result list
res = [ele for sublist in res for ele in sublist]
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads