Python – Product of prefix in list


Python comes under the high-level language and the people working with Python can find solutions to the problem using simple concepts and functionalities. The language is composed of several data structures and from the list is the most common one. The elements inside the list can be from any data type like integer, string, or float data type. And the elements are represented inside the square brackets and separated by a comma. Working with the list data structure is the most interesting fundamental.

Product of prefix in list

The list data structures are composed of integer elements and printing the product of prefixes in a list involves the method of multiplying the first with the second element and later stored in the first index place.

0

1

2

3

5

6

7

8

The first row indicates the index value and the second row represents the element’s value. The first element retains its place. The index value 0 and 1 are multiplied later the result is stored in second place and the process continues.

First element = 5

Second Element = 5*6 = 30

Third Element = 30*3 = 210

Fourth Element = 210*8 = 1680

Approach

  • Approach 1 − Utilizing the User defined function.

  • Approach 2 − Using the numpy module

  • Approach 3 − Using the itertools method

Approach 1: Python Program to Print the product of prefix in a list by utilizing the user function definition

The function is defined with the required parameters to print the product of elements from prefix.

Algorithm

  • Step 1 − The function is defined with one argument.

  • Step 2 − Initializing the empty list and empty variable is created with a value of 1.

  • Step 3 − The for loop will iterate through the list and the other for loop will iterate from the current index value

  • Step 4 − The temporary variable is multiplied by the current element.

  • Step 5 − Then append function will add the elements after multiplying.

  • Step 6 − The multiplied element is returned as a new list using the print function.

Example

#Defining the function with the input of integer data type 
def pre_mul(list1):
#Creating an empty list
    result = []
#for loop is used to iterate through the list according to the range
    for num in range(0,len(list1)):
        temp = 1
        for val in range(num+1):
            temp *= list1[val]
        result.append(temp)
    return result
#list is initialized with elements
list1 = [5, 6 ,7 ,8]
#print function returns the list after the product of prefix
print(pre_mul(list1))

Output

[5, 30, 210, 1680]

Approach 2: Python Program to Print the product of prefix in a list using the numpy module

The prefix elements are multiplied in a particular order from the first element to the last element using the function named “cumprod” from the numpy module.

Algorithm

  • Step 1 − The numpy module is imported to use the required function.

  • Step 2 − The list data structure is created to hold the integer values.

  • Step 3 − The function is used to get the cumulative product of the elements in the list using the cumprod() method

  • Step 4 − The result is stored in the variable named “pro”.

  • Step 5 − Then the print function will return the list after performing the product of prefix.

Example

#Importing the numpy module
import numpy as np
#creating a list to hold the values of integer elements
list1 = [5, 6 ,7 ,8]
#pro variable is created to store the result after cumprod()
pro = np.cumprod(list1)
#return the new list
print(pro)

Output

[   5   30  210 1680]

Approach 3: Python Program to Print the product of prefix in a list using the itertools module

The product of the prefix is printed using the function from the itertools module. This module is generally used to minimize the complexity of iteration through the programs.

Algorithm

  • Step 1 − The itertools module is imported to use the function called accumulate().

  • Step 2 − The list is initialized with four integer elements that is [5, 6, 7, 8].

  • Step 3 − The new variable is declared to hold the value after performing the accumulate operation.

  • Step 4 − The lambda function is generally used, where there is no need to define the function and the process is carried out using the key parameters.

Example

#the required module is imported
import itertools
#list is initialized with integer elements
list1 = [5, 6 ,7 ,8]
#accumulate function to do the multiplication of the prefix elements
pro = list(itertools.accumulate(list1, lambda a,b: a*b))
#finally returns the final list
print(pro)

Output

[5, 30, 210, 1680]

Conclusion

The elements that are once defined inside the list data structure that is within the square brackets cannot be changed. To print the list data structure in the product of prefix, the Python language provides the user with some inbuilt functionalities. So inside the list, we can store multiple values together and thus reducing the complex operations.

Updated on: 04-Sep-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements