I have two parallel lists of data like:
genres = ["classic", "pop", "classic", "classic", "pop"]
plays = [500, 600, 150, 800, 2500]
I want to get this result:
album = {"classic":{0:500, 2:150, 3:800}, "pop":{1:600, 4:2500}} # want to make
So I tried this code:
album = dict.fromkeys(genres,dict())
# album = {'classic': {}, 'pop': {}}
for i in range(len(genres)):
for key,value in album.items():
if genres[i] == key:
album[key].update({i:plays[i]})
The result for album
is wrong. It looks like
{'classic': {0: 500, 1: 600, 2: 150, 3: 800, 4: 2500},
'pop': {0: 500, 1: 600, 2: 150, 3: 800, 4: 2500}}
That is, every plays
value was added for both of the genres, instead of being added only to the genre that corresponds to the number.
Why does this occur? How can I fix the problem?