Martin Spasov
Martin Spasov

Reputation: 313

String formatting python KeyError

What is the function of the list string in the code below:

cities = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data['list']]

i am asking because in the method found_location_name it works

def found_location_name(self, request, data):
    data = json.loads(data.decode()) if not isinstance(data, dict) else data
    cities = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data['list']]
    self.search_results.item_strings = cities

while here i get KeyError:

def found_location_coordinates(self,request_coordinates,data_coordinates):
    data_coordinates = json.loads(data_coordinates.decode()) if not isinstance(data_coordinates, dict) else data_coordinates 
    cities_coordinates = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data_coordinates['list']]
    self.search_results.item_strings = cities_coordinates


Traceback (most recent call last):
File "main.py", line 39, in <module>
 WeatherApp().run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/app.py", line 792, in run
 runTouchApp()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/base.py", line 481, in runTouchApp
 EventLoop.window.mainloop()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/core/window/window_pygame.py", line 381, in mainloop
 self._mainloop()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/core/window/window_pygame.py", line 287, in _mainloop
 EventLoop.idle()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/base.py", line 321, in idle
 Clock.tick()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 422, in tick
 self._process_events()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 537, in _process_events
 if event.tick(self._last_tick) is False:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 309, in tick
 ret = callback(self._dt)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/network/urlrequest.py", line 376, in _dispatch_result
 func(self, data)
File "main.py", line 26, in found_location_coordinates
 cities_coordinates = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data_coordinates['list']]
KeyError: 'list'

Upvotes: 0

Views: 2449

Answers (1)

s16h
s16h

Reputation: 4855

data['list'] means that data is a dictionary and you want the value of the list key. So, data may look like so:

data = {
    'list': [1, 2, 3, 4, 5]
}

Then,

>>> data['list']
[1, 2, 3, 4, 5]

Now, if you try to access a key that doesn't exist, a KeyError will be raised:

>>> data['qwerty']
<omitted traceback>
KeyError: 'qwerty'

So, the data_coordinates dictionary does not have a list key. If the data_coordinates may sometimes have a list key and sometimes not, you can use get() method that returns None or a specified default value if the key doesn't exist:

>>> data.get('qwerty')
None
>>> data.get('qwerty', 'default value')
'default value'

Upvotes: 4

Related Questions