Python Forum
get method within max function for a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get method within max function for a dictionary
#2
Suppose that you want to find the person in a group who owns the most expensive car. In python, you would write it like so
person = max(group, key=price_of_ones_car)
Here group is an iterable of people, such as a list or a dict which keys are people, and price_of_ones_car is a function that returns the price of a person's car
def price_of_ones_car(person):
    return ...
Notice that you don't call the function when you call max(). The function will be called for each person by the max function, but you don't need to do it yourself.
Reply


Messages In This Thread
RE: get method within max function for a dictionary - by Gribouillis - Jul-03-2020, 12:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use value in dictionary as arguments in function gabejohnsonny21 6 3,899 Apr-22-2020, 04:53 PM
Last Post: deanhystad
  Why is this function printing the number of keys in a dictionary? mafr97 5 3,171 Sep-17-2019, 06:19 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020