Python Program to get Maximum product of elements of list in a 2D list
Last Updated :
22 Jan, 2025
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)
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])
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.
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)
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)
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)
Explanation:
- Each element in the sublist is multiplied to calculate the product.
- The maximum product and corresponding list are tracked in variables.
Similar Reads
Python Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples:Â Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi
5 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python program to find N largest elements from a list Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
5 min read
Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e
3 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 | Maximum of Product Pairs in Tuple List Sometimes, while working with data, we might have a problem in which we need to find maximum product between available pairs in list. This can be application to many problems in mathematics domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using max() + list compreh
5 min read