-1

I have the following dictionary of cities with their associated longitudes and latitudes:

CityCoords = {
'Denvor': {'lat': 9, 'lon': 8.5},
'Chicago': {'lat': 12.0, 'lon': 10.2},
'Frisco': {'lat': 10, 'lon': 9.5},
'Boston': {'lat': 11.5, 'lon': 11.0},
}

How do I obtain the name of the cities with the highest latitude and lowest longitude?

RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34
Faramarz
  • 11
  • 2
  • 4
    Possible duplicate of [Getting key with maximum value in dictionary?](http://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – Arya McCarthy Apr 22 '17 at 14:02

1 Answers1

0

You can use the key argument to max() or min() to define an appropriate key, e.g.:

In [1]:
CityCoords = {'Denvor': {'lat': 9, 'lon': 8.5}, 
              'Chicago': {'lat': 12.0, 'lon': 10.2},
              'Frisco': {'lat': 10, 'lon': 9.5},
              'Boston': {'lat': 11.5, 'lon': 11.0}}
max(CityCoords, key=lambda k: CityCoords[k]['lat'])

Out[1]:
'Chicago'

In [2]:
min(CityCoords, key=lambda k: CityCoords[k]['lon'])

Out[2]:
'Denver'

If you need the maximum distance between lat, lon then you could use

In [3]:
max(CityCoords, key=lambda k: CityCoords[k]['lat'] - CityCoords[k]['lon'])

Out[3]:
'Chicago'
AChampion
  • 29,683
  • 4
  • 59
  • 75