• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Lambda Expression with max() in Python

Using Lambda Expression with max() in Python

June 2, 2022 Leave a Comment

The Python max() function allows you use a lambda expression to apply a function to the elements of a list before finding the maximum value. You can easily define a lambda expression and use it with max().

example_list = [0, 3, 1, -3, -5, 4]

print(max(example_list, key=lambda x:abs(x)))

#Output:
-5

When working with collections of data, the ability to find the maximum value of the collection can be useful.

The Python max() function allows you to find the maximum value of different objects.

One cool thing is that you can pass a lambda expression to the “key” parameter to instruct max() what to use to find the maximum value.

For example, if we have a list of numbers and want to take the maximum after applying the absolute value to each element, we can use a lambda.

Below shows a simple example of using a lambda expression with max() in Python.

example_list = [0, 3, 1, -3, -5, 4]

print(max(example_list, key=lambda x:abs(x)))

#Output:
-5

Using Lambda Expression with max() Function on List of Objects in Python

One use of a lambda expression with max() is if you want to find the maximum value in list of objects.

For example, let’s say we have a list of dictionaries and want to find the maximum value over our list for the key “height”.

You can easily do this with “key” and a lambda expression.

Below is an example of how to use a lambda expression with max() to get the maximum value of a list of dictionaries for a given key.

data = [{"name":"Andrew", "age":10, "height":50}, {"name":"Sally", "age":9, "height":45}, {"name":"Jose", "age":11, "height":52}]

print(max(data,key=lambda x: x["height"]))

#Output:
{'name': 'Jose', 'age': 11, 'height': 52}

If you have a list of tuples, you can use “key” and a lambda as well to get the maximum value of a given position in the tuples.

Below is an example of how to use a lambda expression with max() to get the maximum value of a list of tuples for the first element.

data = [(0,'a'),(3,'d'),(2,'r'),(1,'m')]

print(max(data, key=lambda x: x[0]))

#Output:
(3, 'd')

Using Lambda Expression with min() Function in Python

If you want to go the other way and use a lambda expression with the Python min() function, you can do the same as above.

From the first example, we can find the minimum of a list of numbers after taking the absolute value in the same way as we did before.

Below shows you how to use a lambda expression with min() in Python.

example_list = [0, 3, 1, -3, -5, 4]

print(min(example_list, key=lambda x:abs(x)))

#Output:
0

Hopefully this article has been useful for you to learn how to use a lambda expression with max() in Python.

Other Articles You'll Also Like:

  • 1.  Writing a Table Faster to Word Document Using python-docx
  • 2.  pandas min – Find Minimum Value of Series or DataFrame
  • 3.  pandas Drop Columns – Delete Columns from a DataFrame
  • 4.  Check if Character is Letter in Python
  • 5.  Get Last Day of Month Using Python
  • 6.  How to Return Nothing in Python from Function
  • 7.  Using Python to Remove Apostrophe From String
  • 8.  Python os.sep – Create Operating System Path Seperator Character
  • 9.  Replace Values in Dictionary in Python
  • 10.  How to Declare Variable Without Value in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy