I am learning Python, and I have the following problem, I must get the item with the highest value of a dictionary.
verse_dict = {'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, 'don\'t': 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1}
Try this max(verse_dict.values())
that returns the value but I don't know how to get itkey
POSSIBLE ANSWER
This code worked for me, but if there is any way to make it more efficient , I would like to know it.
result, val = 0, ''
for i, j in verse_dict.items():
if result < j:
result = j
val = i
print(val)
The function
max()
is much more powerful and versatile than it seems, thanks to the fact thatkey
a function can be passed through its parameter based on which the maximum will be chosen.What happens is that it
max()
will apply that function to each of the elements of the iterable whose maximum you are trying to obtain, and it will be left with the one for which the result returned by the function is the largest possible.We can use this in the following way. First, you pass
verse_dict.items()
as an iterable to the functionmax()
. As you know this will produce an iterable in which each element is a tuple(clave, valor)
. Without using the optional parameterkey
,max()
it will compare each of these tuples with each other, to keep the "largest" one. One tuple is greater than another if its first element is greater (and in case it is equal, then it will look at the second). This does not work for us because the resulting tuple will be the one that has the maximum value in theclave
, and not in thevalor
.But if we assign to
key
a function that returns the second element of the tuple, then the maximum will be computed based on that second element. In that case the resulting value will(clave, valor)
be the one with thevalor
maximum.The function that needs to be passed to it would be as simple as this:
And therefore we could call in
max()
the following waymax(verse_dict.items(), key=obtener_valor)
. The result will be the tuple(clave, valor)
whosevalor
is maximum. Just keep the key:The de la thing
lambda
is just a way to save yourself from writing a separate function and coming up with a name for it. When the function you need to write is so simple that it consists only of areturn
, you can write it inlambda
the form of where you need it:In this case, the lambda receives a single parameter
x
, which will be each of the elements inverse_dict.items()
, and will returnx[1]
, that is, its value. Note that thelambda
is not putreturn
, but simply the expression that you want to return.Bonuses
The fact of having to write a lambda to keep an element of a tuple is so common that the Python standard library provides a function for this, very useful for the , or functions
max()
,min()
allsort()
of which allow the parameterkey
based on which make the comparisons.In the module
operator
you have the functionitemgetter()
to which you pass as a parameter which element of the tuple you want and it returns another function that you can use instead of the lambda. That returned function will in turn receive a tuple and return the desired element from it.For example:
Even simpler version
ChemaCortes proposes the following solution, even simpler:
I must say that I was thinking about something like that but I couldn't find it, thanks Chema!
How does this case work? We
max()
are passingverse_dict
to , instead ofverse_dict.items()
, so this timemax
it will iterate through only the list of keys, instead of the list of(clave,valor)
. The result will therefore be a key directly, which avoids having to apply the[0]
to the result.For each key in the dictionary,
max
it will call the function specified in the parameterkey
. Thus, the first key obtained will applyverse_dict.get(primera_clave)
, so you will get its value. And so with each of the keys. Since it is these values that are used in the comparisons, the final result will be the key whose value is the maximum.