Reputation: 67
I'm storing values in a dict in this way:
def fname():
return max({(x): x**2 for x in range(1, 20)})
The problem is this returns the value of max key, and i need the value of that key.
How the get the value or rewrite this so that i do get the value of the max key? Preferably without the use of itertools.
EDIT: I forgot to mention, i would need this written in a single line.
Upvotes: 0
Views: 457
Reputation: 6034
As it is requested to print the value of largest key, we have to find the largest key.
l = sorted(list({x:x**2 for x in range(1, 20)}.items()), key = lambda v:v[0], reverse=True)[0][1]
print(l)
# 361
In the above example, largest value is also having largest key. Hence let's solve by different example
d = {1:4,2:3}
l = sorted(list(d.items()), key = lambda v:v[0], reverse=True)[0][1]
print(l)
# 3
Note it prints 3 and not 4.
Upvotes: 0
Reputation: 3706
you can get a list of the values in the dict
max({(x): x**2 for x in range(1, 20)}.values())
361
Upvotes: 1