Reputation: 1000
I have the following dictionary full of city names as keys, fake coordinates stored within a tuple, and corresponding cumulative pairwise distance values. I am trying write a function that returns the dictionary items for the key with the smallest distance value, 29.550506953719747 in this case.
My dictionary:
d = {'Las_Vegas': ((11.0, 10.0), 31.571777946711684),
'Los_Angeles': ((4.0, 8.0), 29.550506953719747),
'San_Diego': ((7.0, 5.0), 32.02332325098408),
'Seattle': ((3.0, 26.0), 57.29385852375022)}
Desired output:
'Los_Angeles': ((4.0, 8.0), 29.550506953719747)
Current approach:
min(d.items(), key = d.get)
This returns:
('Seattle', ((3.0, 26.0), 57.29385852375022))
Upvotes: 0
Views: 40
Reputation: 15081
This should work:
min(d.items(), key=lambda x: x[1][1])
The problem with your current solution is that d.items()
doesn't return the keys of d
, so d.get
always returns None
when called, so min
can't really do its job.
x[1]
will get the dict value for the the key (x[0]
) and x[1][1]
will then access the second value of the tuple (the one you care about).
Upvotes: 4