Python – Row with Maximum Product

Last Updated : 10 Apr, 2023

We can have an application for finding the lists with the maximum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development.

Method #1 : Using reduce() + lambda The above two function can help us achieving this particular task. The lambda function does the task of logic and iteration and reduce function does the task of returning the required result. Works in python 2 only.

Python

 `# Python code to demonstrate` `# Row with Maximum Product` `# using reduce() + lambda`   `# getting Product` `def` `prod(val) :` `    ``res ``=` `1` `    ``for` `ele ``in` `val:` `        ``res ``*``=` `ele` `    ``return` `res `   `# initializing matrix ` `test_matrix ``=` `[[``1``, ``3``, ``1``], [``4``, ``5``, ``3``], [``1``, ``2``, ``4``]]`   `# printing the original matrix` `print` `("The original matrix ``is` `: " ``+` `str``(test_matrix))`   `# using reduce() + lambda` `# Row with Maximum Product` `res ``=` `reduce``(``lambda` `i, j: i ``if` `prod(i) > prod(j) ``else` `j, test_matrix)`   `# printing result` `print` `("Maximum Product row ``is` `: " ``+` `str``(res))`

Output :

```The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Maximum Product row is : [4, 5, 3]```

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #2 : Using max() + key The max function can get the maximum of all the list and key is used to specify on what the max condition has to be applied that is product in this case.

Python3

 `# Python3 code to demonstrate` `# Row with Maximum Product` `# using max() + key`   `# getting Product` `def` `prod(val) :` `    ``res ``=` `1` `    ``for` `ele ``in` `val:` `        ``res ``*``=` `ele` `    ``return` `res `   `# initializing matrix ` `test_matrix ``=` `[[``1``, ``3``, ``1``], [``4``, ``5``, ``3``], [``1``, ``2``, ``4``]]`   `# printing the original matrix` `print` `("The original matrix ``is` `: " ``+` `str``(test_matrix))`   `# using max() + key` `# Row with Maximum Product` `res ``=` `max``(test_matrix, key ``=` `prod)`   `# printing result` `print` `("Maximum product row ``is` `: " ``+` `str``(res))`

Output :

```The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Maximum Product row is : [4, 5, 3]```

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #3: Using numpy

Note: Install numpy module using command “pip install numpy”

Numpy is a python library which provide a lot of mathematical operation. It can help to find the maximum product of row in matrix.

Python3

 `# Python3 code to demonstrate` `# Row with Maximum Product` `# using numpy` `import` `numpy as np` `  `  `# initializing matrix ` `test_matrix ``=` `[[``1``, ``3``, ``1``], [``4``, ``5``, ``3``], [``1``, ``2``, ``4``]]` `  `  `# printing the original matrix` `print` `(``"The original matrix is : "``)` `print``(test_matrix)` `  `  `# using numpy` `# Row with Maximum Product` `res ``=` `test_matrix[np.argmax(np.prod(test_matrix, axis``=``1``))]` `  `  `# printing result` `print` `(``"Maximum product row is : "` `+` `str``(res))` `#This code is contributed by Edula Vinay Kumar Reddy`

Output:
The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Maximum Product row is : [4, 5, 3]

Time Complexity: O(n) where n is the length of the matrix.
Auxiliary Space: O(1)

This method is more efficient than Method 1 and Method 2 because it uses the built-in function in numpy library to find the maximum product of row, so it is more readable and faster.

Next