Home > Blockchain >  Match between dictionaries in list
Match between dictionaries in list

Time:04-23

I have a question to how match between dictionaries

I send 3 request to youtube api

  • first to search
  • second to video
  • third to channel

What I try to create is dic that have

  • Title of the video
  • Thumbnails of the video
  • and the profile of the channel that make the video.

here you can see the code and the requests I send:

 def get(self,request):
    search_url = "https://www.googleapis.com/youtube/v3/search"
    video_url = "https://www.googleapis.com/youtube/v3/videos"
    channel_url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id=' commaSeperatedList '&fields=items(id,snippet/thumbnails)&key={}".format(settings.YOUTUBE_DATA_API_KEY)


    para_search = {
      'part': 'snippet',
      'q': 'Learn Python' ,
      'key': settings.YOUTUBE_DATA_API_KEY,
      'maxResults': 3,
      'type': 'video'

    }

    search_response = requests.get(search_url,params=para_search)
    
    print(search_response.text)
    results = search_response.json()['items']
    ids =[]
    for result in results:
      ids.append(result['id']['videoId'])


    para_videos = {
      'part': 'snippet',
      'key': settings.YOUTUBE_DATA_API_KEY,
      'id':','.join(ids),
    }
    video_response = requests.get(video_url, params=para_videos)
    print(video_response.text)
    results = video_response.json()['items']
    dict_youtube  = {}
    list_youtube = []
    channelIdList = []
    for result in results:
      dict_youtube  = {
         'title': result['snippet']['title'],
         'thumbnails': result['snippet']['thumbnails']['high']['url'],
         'channelId': result['snippet']["channelId"],
       }
      channelIdList.append(result['snippet']["channelId"])
      list_youtube.append(dict_youtube)



    param_channel = {
      'part':'snippet,contentDetails,statistics',
      'key':settings.YOUTUBE_DATA_API_KEY,
      'id':','.join(channelIdList)
    }
    channel_response = requests.get(channel_url,params=param_channel)
    print(channel_response.text)
    results = channel_response.json()['items']
    profile = []
    profile_dic = {}
    for result in results:
      profile_dic = {
        'channelId': result['id'],
        'profile': result['snippet']['thumbnails']['default']['url'],
      }
      profile.append(profile_dic)
    print(profile)
    print(list_youtube)

Input:

profile = [{'channelId': 'UC8butISFwT-*******', 'profile': 'https://yt3.ggpht.com/ytc/A*******ifQn-nYNfkgLvVPkw=s88-********-no-rj'}, {'channelId': 'UCWv7*******mDpPBA', 'profile': 'https://yt3.ggpht.com/tBEPr-zTNXEeae7VZK******2PXSwzMBKVR7W0MI7gyND8=s88-c-k-c0x00ffffff-no-rj'}]

list_youtube = [{'title': 'Learn Python - Full Course for Beginners [Tutorial]', 'thumbnails': 'https://i.ytimg.com/vi/rf****bw/hqdefault.jpg', 'channelId': 'UC******wT-Wl7EV0hUK0BQ'}, {'title': 'Python for Beginners - Learn Python in 1 Hour', 'thumbnails': 'https://i.ytimg.com/vi/kqt*****8/hqd****t.jpg', 'channelId': 'UCWv7*********pPBA'}, {'title': 'Python Tutorial - Python Full Course for Beginners', 'thumbnails': 'https://i.ytimg.com/vi/_uQrJ0TkZlc/hqdefault.jpg', 'channelId': 'U********PBA'}]

As you can see, I've created two lists, each with dictionaries.

And every dictionary has a common key I created and it is channelId

What I want to do is union between dictionaries having the same value in the channelId key, the first list has fewer dictionaries than the second list.

How do I combine the two lists and dictionaries so that everything is compatible That eventually I will have a list with dictionaries that has the key

  1. 'title':
  2. 'thumbnails':
  3. 'channelId':
  4. 'profile':

something like that for example:

[{'title':.... , 'thumbnails':... , 'channelId':... , 'profile':... ,} , { ... }, ...]

CodePudding user response:

That sounds like the classical join operation. You can use a filter to identify the elements that match by id and then update the dictionary with its values like so.

This assumes you have at most 1 video per profile. You might need to flip the variables/add logic depending on their relation

for dic in profile:
    vids = filter(lambda yt: yt["channelId"] == dic["channelId"], list_youtube)
    for vid in vids:
        dic.update(vid)
    return dic
  • Related