Python – Multiply Consecutive elements in list
Last Updated :
14 Apr, 2023
While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are successive product in the list.
Method #1 : Using list comprehension Naive method can be used to perform, but as this article discusses the one liner solutions to this particular problem, we start with the list comprehension as a method to perform this task.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ("The original list is : " + str (test_list))
res = [test_list[i] * test_list[i + 1 ] for i in range ( len (test_list) - 1 )]
print ("The computed successive product list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]
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 #2 : Using zip() zip() can also be used to perform the similar task and uses the power of negative indices to zip() the index element with its next element and hence compute the product.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ("The original list is : " + str (test_list))
res = [i * j for i, j in zip (test_list[: - 1 ], test_list[ 1 :])]
print ("The computed successive product list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]
Time complexity: O(n), where n is the length of the input list, since we are iterating through the list once to create the successive product list.
Auxiliary space: O(n), since we are creating a new list to store the successive product list. The size of this new list will be n-1, since there are only n-1 consecutive pairs in the input list.
Method#3: Using Recursive method.
Python3
def consecutive_product(test_list, index = 0 ):
if index = = len (test_list) - 1 :
return []
return [test_list[index] * test_list[index + 1 ]] + consecutive_product(test_list, index + 1 )
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is:" , test_list)
result = consecutive_product(test_list)
print ( "The computed successive product list is:" , result)
|
Output
The original list is: [1, 4, 5, 3, 6]
The computed successive product list is: [4, 20, 15, 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4:Using enumeration
Algorithm:
- Initialize an empty list called res to store the computed successive product.
- Iterate over the list using the enumerate() function, which provides the current index and its corresponding value in each iteration.
- For each index except the last, multiply the current element with the next element, and append the result to the res list.
- Return the res list.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is : " , test_list)
res = [test_list[i] * val for i, val in enumerate (test_list[ 1 :])]
print ( "The computed successive product list is : " , res)
|
Output
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]
Time Complexity: O(n), where n is the length of the input list. This is because the algorithm iterates over the input list only once to compute the successive product.
Auxiliary Space: O(n), where n is the length of the input list. This is because the algorithm stores the computed successive product in a list of size n-1.
Method #5: Using a for loop
- Initialize an empty list named “res”.
- Iterate over the indices of the input list “test_list” from 0 to the second last index.
- For each index i, multiply the i-th element of “test_list” with the (i+1)-th element of “test_list”, and append the result to the “res” list.
- Return the “res” list.
- Initialize an input list named “test_list” with some integer values.
- Print the original value of the input list.
- Call the function that implements the above approach and store the result in a variable named “res”.
- Print the final consecutive product list “res”.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is : " , test_list)
res = []
for i in range ( len (test_list) - 1 ):
res.append(test_list[i] * test_list[i + 1 ])
print ( "The computed successive product list is : " , res)
|
Output
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]
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 (used for storing the output list).
Method #6: Using a while loop
Step-by-step approach:
- Initialize two variables, i and result, to zero.
- Create an empty list, res.
- While i is less than the length of the test_list subtracted by one, repeat the following steps:
a. Set result equal to the product of test_list[i] and test_list[i+1].
b. Append result to the res list.
c. Increment i by 1.
- Print the computed successive product list, res.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is : " + str (test_list))
i = 0
res = []
while i < len (test_list) - 1 :
result = test_list[i] * test_list[i + 1 ]
res.append(result)
i + = 1
print ( "The computed successive product list is : " + str (res))
|
Output
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]
Time Complexity: O(n), where n is the length of the input list test_list.
Auxiliary Space: O(n), where n is the length of the input list test_list.
Method #7: Using heapq:
Algorithm:
- Initialize an empty list res.
- Loop through the given list test_list from index 1.
- Calculate the product of the current element and its previous element.
- Push the calculated product to the res list using heapq.heappush().
- Repeat steps 3-4 for all elements except the first one.
- The res list will now contain the computed successive product list.
Python3
import heapq
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is : " , test_list)
res = []
for i, val in enumerate (test_list[ 1 :]):
heapq.heappush(res, test_list[i] * val)
print ( "The computed successive product list is : " , res)
|
Output
The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 18, 15, 20]
Time Complexity: O(nlogn), where n is the length of the given list. This is because heapq.heappush() has a time complexity of O(logn), and it is called n-1 times in the loop.
Space Complexity: O(n), where n is the length of the given list. This is because the res list stores n-1 elements. The heapq module also uses additional memory to store the heap, but it is usually not significant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...