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.
Method 1: Get dictionary keys as a list using dict.keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.
Method 2: Extract specific keys from the dictionary using dict() The dict() function can be used to perform this task by converting the logic performed using list comprehension into a dictionary.
Method 1 : Using List. Step 1: Convert dictionary keys and values into lists. Step 2: Find the matching index from value list. Step 3: Use the index to find the appropriate key from key 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:
import urllib2
import json
url = 'http://api.twitter.com/1/trends/44418.json'
# download the json string
json_string = urllib2.urlopen(url).read()
# de-serialize the string so that we can work with it
the_data = json.loads(json_string)
# get the list of trends
trends = the_data[0]['trends']
# print the name of each trend
for trend in trends:
print trend['name']
Or you can do it all in one line:
names = [trend['name'] for trend in the_data[0]['trends']]
for name in names:
print name
Both will result in:
#throwagrenade Rebecca Black Eric Abidal #questionsihate #juniordoctors Smiley Culture Lily Allen Wes Brown Pandev Ray Wilkins
Relevant reading:
Python docs on json (although you should only really need json.loads()
)
Dive Into Python's sections on lists and dictionaries.
Well, for a start, that link gives you JSON, so you'll need to deserialize it with the json
library:
data = json.loads(response_data)
Now you simply have a list of dictionaries. You can easily iterate through the list with a for
loop. On each iteration, you have a normal dictionary, from which you can get the value corresponding to the name
key with the usual dictionary syntax.
You can do the whole thing at once with a simple list comprehension:
names = [item['name'] for item in data]
import urllib2
import json
url = 'http://api.twitter.com/1/trends/44418.json'
data = urllib2.urlopen(url).read()
j = json.loads(data)
names = [d['name'] for d in j[0]['trends']]
results in
names = [u'#throwagrenade', u'Rebecca Black', u'#questionsihate',
u'#thingsthatdontgotogether', u'Eric Abidal', u'Smiley Culture',
u'Ray Wilkins', u'Wes Brown', u'Twenty Twelve', u'Marseille']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With