I want to lookup values ("uri") from a reference dict "fulltracks" and create a new dict with updated values. Each list is going to be used to create playlists with the spotify API afterwards.
Consider this list of dictionaries, each list item displays a track:
fulltracks = [{"artists":[1,2], "uri": "xyz",}
{"artists":[3], "uri": "abc"},
{"artists":[4], "uri": "nmk"},
{"artists":[5], "uri": "qwe"},
Additionally, I have this dictionary where the values are keys (artist) from fulltracks:
genres = {
"rock": [1],
"pop": [2],
"hip hop": [3,4],
"rap": [4],
"house": [5]}
I would like to come out at an updated version of the dictionary "genres_uris":
genres_uris = {
"rock": ["xyz"],
"pop": ["xyz"],
"hip hop": ["abc", "nmk"],
"rap": ["abc"],
"house": ["qwe"]}
I would call it a lookup in Excel but can not get my head into the right keywords for a Python approach/search. I came across pandas for this, is this library the right solution for my problem?
You can use dictionary comprehension:
>>> {k: [d["uri"] for d in fulltracks for val in v if val in d["artists"]] for k, v in genres.items()}
{'rock': ['xyz'],
'pop': ['xyz'],
'hip hop': ['abc', 'nmk'],
'rap': ['nmk'],
'house': ['qwe']}