Having all the movies in a big list is simple, but it is not easy to search. For instance, to find all movies with a certain title, you have to iterate through all of the movies every time. Elon decides to reformat the data into a dictionary with movie titles as the key (see text 55-68). He knows that more than one movie can have the same title, so each value in the dictionary will be a list containing one or more movies. He writes and tests this code:
>>> by_title = {} >>> for movie in m: ... if movie['title'] in by_title: ... by_title[movie['title']].append(movie) ... else: ... by_title[movie['title']] = [ movie ] ... >>> pp(by_title['a star is born']) [{'cast': ['fredric march', 'janet gaynor', 'adolphe menjou'], 'genres': ['drama'], 'title': 'a star is born', 'year': 1937}, {'cast': ['judy garland', 'james mason', 'charles bickford', 'jack carson', 'tommy noonan'], 'genres': ['musical'], 'title': 'a star is born', 'year': 1954}, {'cast': ['barbra streisand', 'kris kristofferson', 'gary busey'], 'genres': ['musical', 'drama'], 'title': 'a star is born', 'year': 1976}, {'cast': ['bradley cooper', 'lady gaga', 'andrew dice clay', 'dave chappelle', 'sam elliott'], 'genres': ['romance', 'drama', 'musical'], 'title': 'a star is born', 'year': 2018}] >>>
Curious about how many times various movies have been made, Elon then does various queries like
>>> for title in by_title: ... if len(by_title[title]) > 5: ... print(title) ... alice in wonderland dr. jekyll and mr. hyde treasure island the three musketeers >>>
which displays what titles have more than 5 entries in the list of movies.
Next he wants to find out if any movies have more than one entry for the same year, but can't figure out how to do it, so he calls you for advice. Can you help him out?
a. Write code to find all of the titles which have more than one entry for the same year. Paste in your code and the titles you find.
b. Look at the complete entries for these titles. What is going on? Were all of these movies actually made more than once in one year? Were any?