Access to information inside a list formatted as a Python dictionary

The Twitter API lists for objects 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's actually a list. Arg.

How can I access certain records? For example, if I want the expand_url value, what is the best way to get it?

Thank.

* Thanks for the quick answers.

+1
source share
3 answers

Index the list in position 0to get a dictionary:

>>> lst = [{'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'}]
>>> lst[0]["expanded_url"]
'http://twitter.com/voxdotcom/status/458708072131592194/photo/1'
>>>
+2
source

What you get is listwith one element of the dictionary inside. Get 0index dictionary :

>>> data = [{'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'}]
>>> type(data)
<type 'list'>
>>> type(data[0])
<type 'dict'>
>>> data[0]['expanded_url']
'http://twitter.com/voxdotcom/status/458708072131592194/photo/1'

- pprint , :

>>> from pprint import pprint
>>> pprint(data)
[{'display_url': 'pic.twitter.com/uc3j0nU8uf',
  'expanded_url': 'http://twitter.com/voxdotcom/status/458708072131592194/photo/1',
  'id': 458708071875764224,
  'id_str': '458708071875764224',
  'indices': [88, 110],
  'media_url': 'http://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png',
  'media_url_https': 'https://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png',
  'sizes': {'large': {'h': 773, 'resize': 'fit', 'w': 1023},
            'medium': {'h': 453, 'resize': 'fit', 'w': 599},
            'small': {'h': 256, 'resize': 'fit', 'w': 340},
            'thumb': {'h': 150, 'resize': 'crop', 'w': 150}},
  'type': 'photo',
  'url': 'http://t.co/uc3j0nU8uf'}]
+1

It looks like you're back - JSON is parsed as a python object. Look carefully - you have a list in which there is only one item.

>>> len([{'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'}])
1

So, all you have to do is take out the first element that you need. If you call this thing my_data, then you want to my_data[0]. It will be a dictionary, and you can access the elements inside it, as usual.

+1
source

Source: https://habr.com/ru/post/1537788/


All Articles