The Twitter API spits out lists for entities that look like this:
[{'expanded_url': 'http://twitter.com/voxdotcom/status/458708072131592194/photo/1', 'display_url': 'pic.twitter.com/uc3j0nU8uf', 'url': 'http://t.co/uc3j0nU8uf', 'media_url_https': 'https://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png', 'id_str': '458708071875764224', 'sizes': {'small': {'h': 256, 'resize': 'fit', 'w': 340}, 'large': {'h': 773, 'resize': 'fit', 'w': 1023}, 'medium': {'h': 453, 'resize': 'fit', 'w': 599}, 'thumb': {'h': 150, 'resize': 'crop', 'w': 150}}, 'indices': [88, 110], 'type': 'photo', 'id': 458708071875764224, 'media_url': 'http://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png'}]
It looks like a dictionary but it is in fact a list. Arg.
How can I access specific entries? For example, if I want the expanded_url value, what is the best way to get it?
Thanks.
*Thanks for the fast replies.
What you get is a
list
with one dictionary item inside. Get the dictionary by0
index:As a side note, pretty-printing with
pprint
helps to see what the data structure consists of:Index the list at position
0
to get the dictionary:It looks like what you got back is JSON parsed as a python object. Look closely - what you have is a list with only one element in it.
So all you need to do is take out the first element, which is the stuff you want. If you're calling this thing
my_data
, then you wantmy_data[0]
. That'll be the dictionary, and you can access the elements inside it as you normally would.