Open In App

Python Program to get Maximum product of elements of list in a 2D list

Last Updated : 22 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a 2D list, we need to find the maximum product of elements for each list within the 2D list and determine which list yields the highest product. The goal is to efficiently compute the product of elements and identify the list with the maximum product.

Using loop with prod from math

This method calculates the product of each list using the prod function from the math module.

Python
from math import prod

# Initializing the 2D list
a = [[2, 3, 5], [1, 4, 2], [6, 1, 1]]

# Initializing variables to store the maximum product and corresponding list
max_product = 0
max_list = []

# Calculating the product of each list
for b in a:
    p = prod(b)  # Finding product using prod function
    if p > max_product:  # Checking if current product is the maximum
        max_product = p
        max_list = b  # Updating the list with the maximum product

print(max_list, max_product)

Output
[2, 3, 5] 30

Explanation:

  • The prod function simplifies the product calculation for each sublist.
  • We maintain variables to track the maximum product and the corresponding list.

Let’s explore some more methods and see how to get maximum product of elements of list in 2D.

Using list comprehension

This method uses list comprehension to calculate the product of each sublist and then determines the maximum.

Python
from math import prod

# Initializing the 2D list
a = [[2, 3, 5], [1, 4, 2], [6, 1, 1]]

# Calculating the maximum product and the corresponding list
products = [prod(b) for b in a]  # List comprehension to calculate product of each sublist
max_index = products.index(max(products))  # Finding the index of the maximum product

print(a[max_index], products[max_index])

Output
[2, 3, 5] 30

Explanation:

  • A list comprehension is used to store the product of each sublist.
  • The index of the maximum product is identified and used to fetch the corresponding list.

Using reduce() from functools

This method uses reduce() from functools to compute the product for each sublist.

Python
from functools import reduce

# Initializing the 2D list
a = [[2, 3, 5], [1, 4, 2], [6, 1, 1]]

# Initializing variables to store the maximum product and corresponding list
max_product = 0
max_list = []

# Calculating the product of each list using reduce
for b in a:
    p = reduce(lambda x, y: x * y, b)  # Using reduce to compute the product
    if p > max_product:  # Checking if current product is the maximum
        max_product = p
        max_list = b  # Updating the list with the maximum product

print(max_list, max_product)

Output
[2, 3, 5] 30

Explanation:

  • The reduce() function applies a lambda function to compute the product of each sublist.
  • This method is concise but less intuitive compared to the previous ones.

Using numpy.prod

This method uses the prod function from the numpy library to compute the product.

Python
import numpy as np

# Initializing the 2D list
a = [[2, 3, 5], [1, 4, 2], [6, 1, 1]]

# Initializing variables to store the maximum product and corresponding list
max_product = 0
max_list = []

# Calculating the product of each list using numpy.prod
for b in a:
    p = np.prod(b)  # Calculating product using numpy.prod
    if p > max_product:  # Checking if current product is the maximum
        max_product = p
        max_list = b  # Updating the list with the maximum product

print(max_list, max_product)

Output
[2, 3, 5] 30

Explanation: numpy.prod computes numpy.prod computes the product efficiently, but importing the library increases overhead.

Using nested loops

This method calculates the product manually by multiplying the elements in each list.

Python
# Initializing the 2D list
a = [[2, 3, 5], [1, 4, 2], [6, 1, 1]]

# Initializing variables to store the maximum product and corresponding list
max_product = 0
max_list = []

# Calculating the product of each list manually
for b in a:
    p = 1  # Initializing product
    for x in b:
        p *= x  # Multiplying elements
    if p > max_product:  # Checking if current product is the maximum
        max_product = p
        max_list = b  # Updating the list with the maximum product

print(max_list, max_product)

Output
[2, 3, 5] 30

Explanation:

  • Each element in the sublist is multiplied to calculate the product.
  • The maximum product and corresponding list are tracked in variables.


Next Article

Similar Reads