Miguel Nuno
Miguel Nuno

Reputation: 135

Python - How to get a specific value from a large list of dicts?

I have a large list of dicts, but I want just a specific information from the list of dict.

How to get the value from key standart: ('standard': {'width': 640, 'height': 480, 'url': 'the url here'}) from this list of dict?

{'snippet': {'videoOwnerChannelId': 'UCG8rbF3g2AMX70yOd8vqIZg', 'channelTitle': 'Logan Paul', 'videoOwnerChannelTitle': 'Logan Paul', 'playlistId': 'PLH3cBjRCyTTxIGl0JpLjJ1vm60S6oenaa', 'description': 'Join the movement. Be a Maverick ► https://ShopLoganPaul.com/\nIn front of everybody...\nSUBSCRIBE FOR DAILY VLOGS! ►\nWatch Previous Vlog ► https://youtu.be/kOGkeS4Jbkg\n\nADD ME ON:\nINSTAGRAM: https://www.instagram.com/LoganPaul/\nTWITTER: https://twitter.com/LoganPaul\n\nI’m a 23 year old manchild living in Los Angeles. This is my life.\nhttps://www.youtube.com/LoganPaulVlogs', 'channelId': 'UCG8rbF3g2AMX70yOd8vqIZg', 'title': 'THE MOST EMBARRASSING MOMENT OF MY LIFE - CHOCH TALES EP. 5', 'resourceId': {'videoId': 'ft6FthsVWaY', 'kind': 'youtube#video'}, 'thumbnails': {'medium': {'width': 320, 'height': 180, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/mqdefault.jpg'}, 'standard': {'width': 640, 'height': 480, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/sddefault.jpg'}, 'high': {'width': 480, 'height': 360, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/hqdefault.jpg'}, 'default': {'width': 120, 'height': 90, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/default.jpg'}, 'maxres': {'width': 1280, 'height': 720, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/maxresdefault.jpg'}}, 'position': 4, 'publishedAt': '2019-03-19T02:00:20Z'}, 'id': 'UExIM2NCalJDeVRUeElHbDBKcExqSjF2bTYwUzZvZW5hYS4wOTA3OTZBNzVEMTUzOTMy', 'etag': 'UU0sUEMm-gC0bcXTF9v1pFV9ZJY', 'kind': 'youtube#playlistItem'}

Upvotes: 0

Views: 130

Answers (2)

Sam Szotkowski
Sam Szotkowski

Reputation: 354

In your example you don't have a list of dictionaries, i.e. [{},{},...] but rather nested dictionaries. You could access that value as follows, if you're sure all the keys must exist:

std_thumb = myDict['snippet']['thumbnails']['standard']
width = std_thum['width']
height = std_thum['height']
url = std_thum['url']

If you're certain that there is only one key in all of the nested dictionaries called "standard," the following would be appropriate too, taken from Get all keys of a nested dictionary :

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield from recursive_items(value)
        else:
            yield (key, value)

for key, val in recursive_items(myDict):
    if key == 'standard':
        print(f'Key: {key}')
        print(f'Val: {val}')

If those keys are always in the top level of nesting, Ahmadreza's answer is certainly appropriate, but in your example this is not the case. My first example is the standard way of indexing a dictionary, and the second is a recursive lookup of a key.

P.S. You don't need if 'standard' in eachDict.keys() you can simply call if standard in eachDict as the "in" keyword already only references keys.

P.P.S. You may find it useful to visualize a complex dictionary using the json library; the following will indent it for you:

import json
print(json.dumps(myDict, indent=4))

Upvotes: 1

Welyweloo
Welyweloo

Reputation: 98

Assuming your dictionary is in a data variable...

  • If you need to extract those data just once you could use :
data['snippet']['thumbnails']['standard']
  • Otherwise you can use this:
for item in data:
    if item == 'snippet':
        for information in data[item]:
            if information == "thumbnails":
                print(data[item][information]['standard'])

>>> Output 
{'width': 640, 'height': 480, 'url': 'https://i.ytimg.com/vi/ft6FthsVWaY/sddefault.jpg'}

Upvotes: 1

Related Questions