Sarah
Sarah

Reputation: 617

How can I create a nested dictionary using a for loop in Python?

I'm creating a youtube downloader for my project, so I've been playing around with YouTube API. I have a problem putting the data into a dictionary, so for example, this is the data:

result = {'kind': 'youtube#searchListResponse', 'etag': '5JMmuS5CVq2hYbanuVXwLKfuwXk', 'nextPageToken': 'CAEQAA', 'regionCode': 'US', 'pageInfo': {'totalResults': 1000000, 'resultsPerPage': 1}, 'items': [{'kind': 'youtube#searchResult', 'etag': '_quYyNz6XPXmAaWfx4pkGm_ZXRA', 'id': {'kind': 'youtube#video', 'videoId': 'Jn09UdSb3aA'}, 'snippet': {'publishedAt': '2020-03-04T12:00:04Z', 'channelId': 'UCyOfqgtsQaM3S-VZnsYnHjQ', 'title': 'The Best of Chopin', 'description': 'Buy the MP3 album on the Official Halidon Music Store:  Listen to our playlist on Spotify: ialClassics Order “100 ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'HALIDONMUSIC', 'liveBroadcastContent': 'none', 'publishTime': '2020-03-04T12:00:04Z'}}]}

    search = {}
    search_data = []

    for item in result['items']:
        title = item['snippet']['title']
        description = item['snippet']['description']
        video = item['id']['videoId']

        search['title'] = title
        search['description'] = description
        search['videoId'] = video
    
        search_data.append(search)

And I have a difficult time adding each data into the dictionary with the "title" as a key. So I want the dictionary to be looks like this:

{'The Best of Chopin': 
         {'description': 'Buy ... : AYhx Listen .... ', 
          'videoId': 'Jn09UdSb3aA'}, 
'The Next Title': 
         {'description': 'blah blah', 
          'videoId':'121212121'}, 
'The Third one.... and goes on

I've been reading multiple articles about the nested dictionary, but I see lots of iterating through but not how to create.. I tried and played around a bit but I wasn't able to get it done. The only close solution that I tried was using "enumerate" in for loop to create with a key, but I'm trying to use the title as the key.. How can I do this? Any advice will be appreciated.

Upvotes: 0

Views: 69

Answers (2)

user2349470
user2349470

Reputation: 198

search_result = {'kind': 'youtube#searchListResponse', 'etag': '5JMmuS5CVq2hYbanuVXwLKfuwXk', 'nextPageToken': 'CAEQAA', 'regionCode': 'US', 'pageInfo': {'totalResults': 1000000, 'resultsPerPage': 1}, 'items': [{'kind': 'youtube#searchResult', 'etag': '_quYyNz6XPXmAaWfx4pkGm_ZXRA', 'id': {'kind': 'youtube#video', 'videoId': 'Jn09UdSb3aA'}, 'snippet': {'publishedAt': '2020-03-04T12:00:04Z', 'channelId': 'UCyOfqgtsQaM3S-VZnsYnHjQ', 'title': 'The Best of Chopin', 'description': 'Buy the MP3 album on the Official Halidon Music Store:  Listen to our playlist on Spotify: ialClassics Order “100 ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/Jn09UdSb3aA/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'HALIDONMUSIC', 'liveBroadcastContent': 'none', 'publishTime': '2020-03-04T12:00:04Z'}}]}
search_data = {}
for item in search_result['items']:
    title = item['snippet']['title']
    description = item['snippet']['description']
    video = item['id']['videoId']
    temp = {}
    temp['description'] = description
    temp['videoId'] = video
    search_data[title] = temp

print(search_data)

Output:

{'The Best of Chopin': {'description': 'Buy the MP3 album on the Official Halidon Music Store:  Listen to our playlist on Spotify: ialClassics Order “100 ...', 'videoId': 'Jn09UdSb3aA'}}

Upvotes: 1

Jarvis
Jarvis

Reputation: 8564

Do it like this:

search[title] = {}
search[title]['description'] = description
search[title]['videoId'] = video

Upvotes: 1

Related Questions