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
#1
I've been working on the Udacity python course, and in of the lessons I needed to take a dictionary of years and lists of names and make a list of the name(s) that appeared the greatest number of times. I was stuck until I found some code online, but I don't understand it and haven't been able to find any clarification as of yet.
The code is as follows:

most_appeared.append(max(mydict,key=mydict.get))
My trouble is with the second argument of max: key=mydict.get

Why doesn't get include the normal parenthesis at the end? How does this argument tell python to return the keys instead of the values? If someone could explain how this works it would be much appreciated.
Reply
#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
#3
I don't use max and min all that much and was not aware you could specify a key. Guess that makes a lot of sense since they do something very similar to sort. Knowing about the key I can think of many times I could have used max and min to make my code shorter and cleaner. Thanks!
Reply
#4
What you've learnt here is that functions can be treated as values in Python and so can be passed around to other functions. This is quite a useful, powerful idea, because it means you can build quite generic functions that can be reused in different ways, by passing in different functions. That seems a bit abstract, so let's look at some examples:

Let's say I have a list of numbers and I want to double each of them, or I have a list of strings and want to turn them into uppercase. The thing in common here is wanting to do something to each item in the list; it's just that the "something" varies. The map function is the kind of generic function I'm talking about. It takes a list (or other iterable) and a function to call on each item:

>>> values = [1, 2, 3, 4]
>>> def double(x):
...     return 2 * x
... 
>>> list(map(double, values))
[2, 4, 6, 8]
>>> words = ["hello", "goodbye", "dog", "cat"]
>>> def uppercase(word):
...     return word.upper()
... 
>>> list(map(uppercase, words))
['HELLO', 'GOODBYE', 'DOG', 'CAT']
>>> 
A couple things to note here:

1. Using map might not be considered the most idiomatic Python - normally I'd use a list comprehension for these but I'm specifically using map to illustrate the point.

2. I have to call list on the return value of map because it doesn't return a list, but rather a kind of lazy sequence. Essentially that means that each of the items in that sequence is computed on demand when it's accessed, so printing the object you get back wouldn't be much use.

This idea of higher order functions (those that either take functions as arguments, or return them) is quite commonplace in many programming languages today.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use value in dictionary as arguments in function gabejohnsonny21 6 3,654 Apr-22-2020, 04:53 PM
Last Post: deanhystad
  Why is this function printing the number of keys in a dictionary? mafr97 5 2,983 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