Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Maximum product using K elements
Next article icon

Python – Row with Maximum Product

Last Updated : 10 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Article
Python - Maximum product using K elements
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Pair with Maximum product
    Sometimes, we need to find the specific problem of getting the pair which yields the maximum product, this can be computed by getting initial two elements after sorting. But in some case, we don’t with to change the ordering of list and perform some operation in the similar list without using extra
    6 min read
  • Python - Maximum product using K elements
    Sometimes, while working with Python lists, we can have a problem in which we need to maximize certain numbers. There can be many conditions of maximizing. Example of that is maximizing product by using K elements. Lets discuss certain ways in which this can be performed. Method #1 : Using max() + s
    4 min read
  • Matrix Product - Python
    The task of calculating the matrix product in Python involves finding the product of all elements within a matrix or 2D list . This operation requires iterating through each element in the matrix and multiplying them together to obtain a single cumulative product. For example, given a matrix a = [[1
    3 min read
  • Python program maximum of three
    The task of finding the maximum of three numbers in Python involves comparing three input values and determining the largest among them using various techniques. For example, if a = 10, b = 14, and c = 12, the result will be 14. Using max()max() is the straightforward approach to find the maximum of
    3 min read
  • Python - Row with Maximum Record Element
    Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
    7 min read
  • Python | Triple Product to K
    The problem of getting the product number of pairs that lead to a particular solution has been dealt many times, this articles aims at extending that to 3 numbers and discussing several ways in which this particular problem can be solved. Let’s discuss certain ways in which this can be performed. Me
    3 min read
  • Python - Cumulative Records Product
    Sometimes, while working with data in form of records, we can have a problem in which we need to find the product element of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed. Method #1 :
    5 min read
  • Keys with Maximum value - Python
    In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to ide
    4 min read
  • Python | Record Index Product
    Sometimes, while working with records, we can have a problem in which we need to multiply all the columns of a container of lists that are tuples. This kind of application is common in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using loop + li
    4 min read
  • Python Program for Maximum Product Subarray
    Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used. Examples: Input: arr[] = {6, -3, -10, 0, 2}Output: 180 // The subarray is {6, -3, -10}Input: arr[] = {-1, -3, -1
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences