Python Forum
How to take a single value from dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to take a single value from dictionary (/thread-18708.html)



How to take a single value from dictionary - ginjaninja247 - May-28-2019

I've created codes that collects weather data using JSON and API keys. However, all I really need is one value of one dictionary, but my code keeps returning "TypeError: list indices must be integers or slices, not str". What do I do.

Relevant Code:
url = "http://api.openweathermap.org/data/2.5/weather?q=brisbane&appid=7f50acdcb4d66b2117f7af48d489fbd4"
response = urllib.request.urlopen(url)
result_3 = json.loads(response.read())
weather = result_3['weather']['description']
print("ISS now at latitude: ", weather)
'weather' dictionary:[{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}]


RE: How to take a single value from dictionary - buran - May-28-2019

weather = result_3['weather'][0]['description']
weather is a list with one element (at least in this case). Maybe in other cases it could have more than one lement