-1

I'm doing some reverse Geo coding to convert latitude and longitude to a location name:

def reverseGeocode(coordinates):
    result = rg.search(coordinates)
    print(result)

if __name__ == "__main__":
    coord = (-33.936952777777776, 18.39005)
    reverseGeocode(coord)

Result has following format:

[OrderedDict([('lat', '-33.92584'), ('lon', '18.42322'), ('name', 'Cape Town'), ('admin1', 'Western Cape'), ('admin2', 'City of Cape Town'), ('cc', 'ZA')])]

How can i get to certain element of this OrderedDict to have just a location 'name'? That would be 'Cape Town' in this example.

Bikramjeet Singh
  • 681
  • 1
  • 7
  • 22
Ale
  • 645
  • 4
  • 16
  • 38
  • 2
    In the same way you retrieve the "name" key from an ordinary dict. An OrderedDict has only a special string representation. – Michael Butscher Jan 04 '20 at 17:59
  • 1
    Or if you want by index: https://stackoverflow.com/questions/10058140/accessing-items-in-an-collections-ordereddict-by-index – sshashank124 Jan 04 '20 at 18:00
  • 3
    Note that result is actually a `list` of `OrderedDict` – sshashank124 Jan 04 '20 at 18:00
  • What is your question, exactly? If you want to know how to use a dictionary, then that's been asked before. As @MichaelButscher pointed out, you have a list of OrderedDicts, it isn't clear if you understand that. – AMC Jan 04 '20 at 18:25

1 Answers1

0

result[0]['name'] # == 'Cape Town'

kingkupps
  • 3,284
  • 2
  • 16
  • 28