0

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.

2
  • 1
    What is search_result? Is that supposed to be result? Commented Dec 25, 2020 at 3:15
  • sorry that was typo!! Commented Dec 25, 2020 at 3:20

2 Answers 2

1

Do it like this:

search[title] = {}
search[title]['description'] = description
search[title]['videoId'] = video
Sign up to request clarification or add additional context in comments.

4 Comments

Or more simply search[title] = {'description': description, 'videoId': video}
Thank you for the advice! But how can I append to search_data? I tried now with search_data.append(search) but it seems like I only have the first data...
It has all the items from your result variable, maybe the input result is incomplete .. @sarahkim you don’t need to append anything to a list, your dictionary will have all the elements anyway
Omg I'm stupid lol you're right, I just created only for 1 output hahaha
1
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'}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.