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 the contents of the list and while doing that, each time new list must add a number to original list. This incremental expansion has applications in many domains. Let’s discuss a way in which this task can be performed.
Method: 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 an addition factor list and then cumulate the result using original list.
Python3
# Python3 code to demonstrate working of# Incremental List Extension# Using list comprehension# initializing listtest_list = [7, 8, 9]# printing original listprint("The original list is : " + str(test_list))# Extension factorN = 4# Addition factorM = 3# Incremental List Extension# Using list comprehensiontemp = [1 * M**i for i in range(N)]temp[0] = 0res = list([ele + tele for tele in temp for ele in test_list])# printing resultprint("List after extension and addition : " + str(res)) |
The original list is : [7, 8, 9] List after extension and addition : [7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]
Time complexity: O(N*M), where N is the extension factor and M is the addition factor.
Auxiliary space: O(N), where N is the extension factor, for the temporary list ‘temp’.
Method 2: using the map() and zip() functions:
In this implementation, we use the map() function to apply a lambda function to each element of the range(N) and test_list lists to generate the temp and res lists, respectively. The zip() function is then used to combine the elements of test_list and temp into pairs, which are then added together using another map() function with a lambda function that adds its two arguments.
- Create a list test_list containing integers 7, 8, and 9.
- Print the original list test_list.
- Set the extension factor N to 4 and the addition factor M to 3.
- Create a list temp using map() and a lambda function that raises M to the power of each element in the range range(N), except for the first element which is set to 0.
- Create a list res using map(), zip(), and another lambda function that adds the corresponding elements of test_list and temp.
- Print the final list res.
Python3
# Python3 code to demonstrate working of# Incremental List Extension# Using map() and zip()# initializing listtest_list = [7, 8, 9]# printing original listprint("The original list is : " + str(test_list))# Extension factorN = 4# Addition factorM = 3# Incremental List Extension using map() and zip()temp = list(map(lambda i: M**i if i > 0 else 0, range(N)))res = list(map(lambda x: x[0]+x[1], zip(test_list * N, temp * len(test_list))))# printing resultprint("List after extension and addition : " + str(res)) |
The original list is : [7, 8, 9] List after extension and addition : [7, 11, 18, 34, 8, 12, 16, 35, 9, 10, 17, 36]
Time complexity: O(N), where N is the size of the output list res.
Auxiliary space: O(N), where N is the size of the output list res.
Method 3: Using a loop to extend the list and then adding a constant value to each element.
Approach:
- Create an empty list named temp to store the values of the incremental list extension.
- Use a loop to create an incremental list of length N using the formula 1 * M**i, where i is the index of the current element. Store the values in the list temp. The first element of the list is set to 0.
- Create an empty list named res to store the final result.
- Use two nested loops to add each element of the test_list with each element of the temp list, and store the result in the res list.
- Print the final list after extension and addition using the print() function.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of# Incremental List Extension# Using loop# initializing listtest_list = [7, 8, 9]# printing original listprint("The original list is : " + str(test_list))# Extension factorN = 4# Addition factorM = 3# Incremental List Extension# Using looptemp = [1 * M**i for i in range(N)]temp[0] = 0res = []for tele in temp: for ele in test_list: res.append(ele + tele)# printing resultprint("List after extension and addition : " + str(res)) |
The original list is : [7, 8, 9] List after extension and addition : [7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]
Time Complexity: O(N^2), where N is the extension factor. The loop used to create the incremental list has a time complexity of O(N), and the nested loop used to add each element of test_list with each element of temp has a time complexity of O(N^2).
Auxiliary Space: O(N), where N is the extension factor. The space required for the temp list is O(N), as it stores the incremental list. The space required for the res list is also O(N^2), as it stores the final result after extension and addition.
Method 4: use the itertools library’s
- Initialize a list of integers called test_list with values [7, 8, 9].
- Print the original list by converting it to a string and adding it to a string message.
- Set two variables called N and M to the values 4 and 3, respectively. These variables are used to determine how much to extend the list and how much to increment each element in the list.
- Generate a list called powers_of_m using a list comprehension that calculates M raised to the power of each integer in the range 0 to N-1. The first element of the list is set to 0 because it will not be used in the final calculation.
- Generate all possible combinations of elements from test_list and powers_of_m using a nested list comprehension. Each combination is formed by adding an element from test_list and an element from powers_of_m. The resulting list of combinations is assigned to a variable called combinations.
- Print the resulting list by converting it to a string and adding it to a string message.
Python3
# initializing listtest_list = [7, 8, 9]# printing original listprint("The original list is : " + str(test_list))# Extension factorN = 4# Addition factor M = 3# generate list of powers of Mpowers_of_m = [M**i for i in range(N)]powers_of_m[0] = 0# generate all combinations of test_list and powers_of_mcombinations = [(x+y) for x in test_list for y in powers_of_m]# printing result print("List after extension and addition : " + str(combinations)) |
The original list is : [7, 8, 9] List after extension and addition : [7, 10, 16, 34, 8, 11, 17, 35, 9, 12, 18, 36]
Time Complexity :O(N^2)
Auxiliary Space: O(N^2)
Method 5: Using numpy to perform vectorized operations.
Step-by-step approach:
Import the numpy library.
Convert the original list to a numpy array.
Create an array of powers of M using numpy’s power function.
Set the first element of the powers of M array to 0.
Use numpy’s broadcasting feature to add the original array to the powers of M array.
Return the resulting array.
Python3
# importing numpy libraryimport numpy as np# initializing listtest_list = [7, 8, 9]# printing original listprint("The original list is : " + str(test_list))# Extension factorN = 4# Addition factor M = 3# create numpy array from listarr = np.array(test_list)# create array of powers of M using numpypowers_of_m = np.power(M, np.arange(N))# set the first element of powers_of_m to 0powers_of_m[0] = 0# add arrays using broadcastingresult = arr[:, np.newaxis] + powers_of_m[np.newaxis, :]# convert result back to listcombinations = result.ravel().tolist()# printing result print("List after extension and addition : " + str(combinations)) |
OUTPUT: The original list is : [7, 8, 9] List after extension and addition : [7, 10, 16, 34, 8, 11, 17, 35, 9, 12, 18, 36]
Time complexity: O(N), where N is the extension factor.
Auxiliary space: O(N), for the array of powers of M created using numpy.
