I received a list when inputting the following URL – http://api.twitter.com/1/trends/44418.json
The list contains multiple dictionaries, and I’m a bit confused with the list structure. I’m trying to obtain the values associated with the ‘name’ key.
For example:
“name”:”#throwagrenade”
“name”:”Rebecca Black”
“name”:”#questionsihate”
I can write the code myself, I’m just trying to conceptually understand how to access dictionaries (and their key/value pairs) within a list.
The first thing I would do when working with a big lump of json, is try to get it into a more readable format. This online json formatting tool should do the job.
Here’s some code that will get all the trend names:
Or you can do it all in one line:
Both will result in:
Relevant reading:
Python docs on json (although you should only really need
json.loads())Dive Into Python‘s sections on lists and dictionaries.