Dictionary code problem. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Dictionary code problem.

#Write a function called stars that takes in two dictionaries: # - movies: a dictionary where the keys are movie titles and the values are lists of major performers in the movie. For example: movies["The Dark Knight"] = ["Christian Bale", "Heath Ledger", "Maggie Gyllenhall", "Aaron Eckhart"] - tvshows: a dictionary where the keys are TV show titles and the values lists of major performers in the show. #For example: tvshows["Community"] = ["Joel McHale", "Alison Brie", "Danny Pudi", "Donald Glover", "Yvette Brown"] #The function stars should return a new dictionary. The keys of the new dictionary should be the performers' names, and the values for each key should be the list of shows and movies in which that performer has appeared. Sort the shows and movies alphabetically. def stars(movies, tvshows): result = {} for (key,value) in movies.items(): for each_value in value: if value not in result.keys(): result[value]= [] result[value].append(key) for (key,value) in tvshows.items(): for each_value in value: if value not in result.keys(): result[value]= [] result[value].append(key) return result movies = {"How to Be Single": ["Alison Brie", "Dakota Johnson", "Rebel Wilson"], "The Lego Movie": ["Will Arnett", "Elizabeth Banks", "Alison Brie", "Will Ferrell"]} tvshows = {"Community": ["Alison Brie", "Joel McHale", "Danny Pudi", "Yvette Brown", "Donald Glover"], "30 Rock": ["Tina Fey", "Tracy Morgan", "Jack McBrayer", "Alec Baldwin", "Elizabeth Banks"], "Arrested Development": ["Jason Bateman", "Will Arnett", "Portia de Rossi"]} print(stars(movies, tvshows)) ******************************************** I am getting error "TypeError: unhashable type: 'list' on line " if value not in result.keys():" please advise

13th Oct 2020, 6:36 PM
NG_
NG_ - avatar
4 Answers
+ 9
NG_ , there is only a small issue but with a big impact on the outcome. you mixed up 2 variable names. see the reworked code in the attached list: https://code.sololearn.com/c6ydvx81wf1Y/?ref=app
13th Oct 2020, 7:46 PM
Lothar
Lothar - avatar
+ 4
https://code.sololearn.com/c7KuTVOrqrJJ/?ref=app
13th Oct 2020, 11:04 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 2
It would be better to save your code in the playground and link it to the question, it would be much easier to test. The problem is you are trying to use a list as dictionary key. That is just not possible. The key should be the name of the star (a string). Maybe you could avoid this confusion by better choosing your variable names, so you remember what they actually mean: for movie, crew in movies.items(): for star in crew: if star not in result.keys(): result[star]= [] result[star].append(movie)
13th Oct 2020, 7:48 PM
Tibor Santa
Tibor Santa - avatar
0
Thank you Lothar for your input. I was struggling with this since yesterday.
14th Oct 2020, 4:46 AM
NG_
NG_ - avatar