Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do it like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...