Kodeclik Blog

Python max() using lambda functions

Python iterables like lists, dictionaries, and strings allow you to traverse them and order them according to any reasonable criterion you might have.
For instance, let us take a Python dictionary denoting items in your school cafeteria and their respective prices:
Note that every item in the dictionary is a (key, value) pair where key is the name of the menu item and price denotes the price of the menu item.
Let us try to find the max() in this collection. But what does max() mean in this context? Do we intend to sort the items alphabetically and pick the one with the largest key (hence the name that appears lexicographically the latest in order) or do we mean to pick the one with the highest price (and thus costliest)?
The nice thing about Python is that it allows you to choose whatever interpretation makes sense to you! You just have to create a lambda function that captures the meaning you have in mind.

Python max without lambda

Let us first try max() on the above iterable without any arguments and see what happens:
This yields:
As you can see a default max() on an iterable is run on the keys (in our case, the names of the menu items) and they are lexicographically ordered and thus the one that appears latest in the alphabet is returned, which is “Pizza Slice”.
If you desire to find the cost of the item that was ranked the max (i.e., “Pizza Slice”), you should update your program as follows:
Note that in the second line, we are first applying max to find the key that is the highest, and then indexing into menu_items to find the cost and then converting it into a string so that we can concatenate it. The output is:
We can clean up the code above a little bit so that we do not compute the max twice in two successive lines:
The output is still:

Python max with lambda = identity function

The above code uses an implicit ordering mechanism to find the max(imum). And we can make this explicit in the following manner:
Note that the max() function has been updated to have a second argument specifying the “key” to sort the collection by. The key is simply set to be a lambda function that is actually the identity function. The inputs to the lambda in this case are the names of the menu items and the lambda simply returns the name, and so the sorting order is by name. If you run this program, you will get the same output as before:

Python max with lambda to sort by price

We can change the lambda to instead sort by the price:
Note that the lambda’s key argument now returns the price as the item to sort by. The output is:
because Fries has the highest price, namely 1.00.

Python max with lambda to sort by length of key

Here is yet another way to sort. Let us sort by the length of the name of the item; in other words longer names appear later so the max() according to this criterion will be the item that has the longest name.
The output is:
because “Fresh Oven-baked Biscuits” has the longest name.

Python max with lambda to sort by number of coins in price

Finally, here is a quirky way to sort. We will convert the price into coins (i.e., quarters, dimes, nickels, and pennies) and use the number of coins as the criterion to sort. For this purpose, we will write two additional functions, one to convert the price to coins and another to count the number of coins needed. Here is how that works:
In the convert_price_to_coins, we take the cost (given as input in cents) and progressively divide it by the largest denomination possible, from quarters to pennies, and accumulate the number of coins.
For instance, if we do:
we will get:
meaning we need 2 quarters and 2 dimes to get 70 cents. You do not need any nickels or pennies. The total coin count is 2+2=4 as printed by the count_coins() function.
In the lambda function, we use count_coins to setup the key (taking care to multiply by 100, because the prices of menu items in the dictionary are given in dollars whereas these functions expect input in cents).
If we run the above program, we get:
because purchasing an Orange requires the most number of coins.
So as you saw from the above examples, we can easily change the lambda in the max() function to define various meanings of what it means to be the maximum and thus create very expressive programs.
Can you think of additional ways to sort by creating your own lambda?
If you liked this blogpost, checkout our blogpost on whether you can create a multiline lambda function. Also learn about functors in Python.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.