Python | Product of Prefix in list
Last Updated :
01 Jun, 2023
Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Let’s discuss certain ways in which this problem can be solved.
Method 1: Using list comprehension + list slicing
This problem can be solved using the combination of the above two functions in which we use list comprehension to extend the logic to each element and then later compute the product, slicing is used to get the product to the particular index.
Python3
def prod(test_list):
res = 1
for ele in test_list:
res = res * ele
return res
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
res = [prod(test_list[: i + 1 ]) for i in range ( len (test_list))]
print ( "The prefix prod list is : " + str (res))
|
Output :
The original list : [3, 4, 1, 7, 9, 1]
The prefix prod list is : [3, 12, 12, 84, 756, 756]
Time complexity: O(n), where n is the length of the test_list. The list comprehension + list slicing takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method 2: Using numpy
Note: Install numpy module using command “pip install numpy”
This problem can be solved by using numpy library which has the cumprod() function that can be used to calculate the cumulative product of elements in the list.
Python3
import numpy as np
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
result = np.cumprod(test_list)
print ( "The prefix prod list is : " + str (result))
|
Output :
The original list : [3, 4, 1, 7, 9, 1]
The prefix prod list is : [3, 12, 12, 84, 756, 756]
Time complexity of this approach is O(n)
Auxiliary space is O(n)
Method 3: Use a loop and create an output list to store the prefix products.
steps
- Initialize an empty output list to store the prefix products.
- Initialize a variable ‘product’ to 1.
- Loop through the input list and multiply the current element with ‘product’ to get the prefix product.
- Append the prefix product to the output list.
- Update the ‘product’ variable to the current element.
- Return the output list.
Python3
def prefix_product(test_list):
output = []
product = 1
for i in test_list:
product * = i
output.append(product)
return output
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
res = prefix_product(test_list)
print ( "The prefix prod list is : " + str (res))
|
Output
The original list : [3, 4, 1, 7, 9, 1]
The prefix prod list is : [3, 12, 12, 84, 756, 756]
Time complexity: O(n) where n is the length of the input list.
Auxiliary space: O(n) to store the output list.
Method 4 : using the accumulate function from the itertools module.
step-by-step approach:
- Import the accumulate function from the itertools module
- Define the prefix_product function with the same logic as before
- The accumulate function takes two arguments: the input list and a function that specifies the operation to be applied between the elements. In this case, we use a lambda function to calculate the product of two elements.
- Convert the result into a list using the list function.
- The final output is the accumulated product of the input list.
Python3
from itertools import accumulate
def prefix_product(test_list):
output = list (accumulate(test_list, lambda x, y: x * y))
return output
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list: " + str (test_list))
res = prefix_product(test_list)
print ( "The prefix prod list is: " + str (res))
|
Output
The original list: [3, 4, 1, 7, 9, 1]
The prefix prod list is: [3, 12, 12, 84, 756, 756]
The time complexity of this approach is O(n), where n is the length of the input list.
The auxiliary space complexity is O(n) because we need to store the prefix product list in memory.
Similar Reads
Python - Product of i^k in List
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of i^k of list in just one line. Letâs discuss
5 min read
Python - Minimum Product Pair in List
Sometimes, we need to find the specific problem of getting the pair that yields the minimum product, this can be solved by sorting and getting the first and second elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in the similar list with
3 min read
Python - Product of consecutive pairs in list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be solved.
7 min read
Python - Cubes Product in list
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of cubes of list in just one line. Letâs discu
5 min read
Python - Suffix Product in list
We are given a list of numbers, and our task is to compute the suffix product for each position in the list. The suffix product at any index is the product of all elements to the right, including the element at that index. For example, given a = [2, 3, 4, 5], the suffix products would be [120, 60, 2
2 min read
Python | Sliced Product in List
Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must know to perform the same. This article discusses ways to fetch the initial K elements and do its multiplication. Letâs discuss a certain solution to perform this task. Method #1
5 min read
Python | Column Product in List of lists
Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Letâs discuss certain ways in which this problem can be solved. Meth
6 min read
Python | Product of kth column in List of Lists
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Usin
4 min read
Python | Pair Product combinations
Sometimes, while working with data, we can have a problem in which we need to perform tuple multiplication among all the tuples in list. This can have application in many domains. Letâs discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension Th
4 min read
Python | Arbitrary List Product
Sometimes, in making programs for gaming or gambling, we come across the task of creating a list with arbitrary numbers and performing its product. This task has to perform in general using a loop and appending the arbitrary numbers one by one and then performing the product. But there is always a r
5 min read