Due: 10:00pm EST on Monday, November 9, 2026.
Instructions: complete this homework using Thonny, because this requires working with external files (and that can be cumbersome in Colab).
Click here to download the Movies folder to get started. This folder contains the movies.csv text file files that you will need to read and analyze its contents. Inside the Movies folder, create a new file called username_movies.py. For example, this is how Saoirse Ronan would name her file: sr112_hw8_movies.py. Write all your code for this assignment, including your testing code, in this file.
Practice reading from a text (csv) file
Practice building and working with dictionaries and a list of dictionaries
Loop through a list of dictionaries
Practice sorting data
You'll read in a data file, create a list of dictionaries, and then perform data analysis on the movie data.
movies.csv
In this task, you are provided with a csv file of information for over 85,000 movies (data from https://grouplens.org/datasets/movielens/). Your task is to build a list of dictionaries from this file, where each dictionary is a movie, and then write code to analyze the movie data.
The file movies.csv is the only data file you will need for this task. You can see a snippet of the file's format at left.
This function has one parameter:
the filename from which to read the data
Returns a list of dictionaries, one dictionary per movie in the file. Each dictionary has three keys: 'id', 'title', and 'genres'. The 'id' key corresponds to an integer value, the 'title' key corresponds to a string title with the year in parentheses, and the 'genres' key corresponds to a list of genres for that particular movie. Here is an example of the structure of the dictionaries in the returned list:
[ { "id": 1,
"title": "Toy Story (1995)",
"genres": ["Adventure", "Animation", "Children", "Comedy", "Fantasy"]
},
{ "id": 2,
"title": "Jumanji (1995)",
"genres": ["Adventure", "Children", "Fantasy"]
},
...
]
Hints:
You may use the linesFromFile function from lecture that, given a filename, reads in one line per file and returns a list of strings.
There are 86536 movies in the movies.csv file, so your list of dictionaries returned by buildMovieList should have a length of 86536, one dictionary for each movie.
This function takes 3 parameters:
the list of movie dictionaries
genre1 (eg "Comedy")
genre2 (eg "Documentary")
and returns the percentage of movies that have both genre1 and genre2 listed as genres in the entire movie list, rounded to the 2nd decimal point.
movieList = buildMovieList('movies.csv')
romComPercent = twoGenrePercentage(movieList, 'Comedy','Romance')
actionAdventurePercent = twoGenrePercentage(movieList, 'Action','Adventure')
dramComPercent = twoGenrePercentage(movieList, 'Drama','Comedy')
docuDramaPercent = twoGenrePercentage(movieList, 'Documentary','Drama')
print(romComPercent)
5.2
print(actionAdventurePercent)
2.5
print(dramComPercent)
6.92
print(docuDramaPercent)
0.4
This function has one parameter:
a list of movie dictionaries
The function prints the movie with the highest number of genres. It prints the highest number of genres and the movie title and the list of genres. If there are movies that tie for the highest number of genres, let's say there are three, your function can return any one of those three movies (it doesn't matter which one). The function only prints; it does not return a value.
Movie with most genres: 10 genres
Rubber (2010): ['Action', 'Adventure', 'Comedy', 'Crime', 'Drama', 'Film-Noir', 'Horror', 'Mystery', 'Thriller', 'Western']
This function takes two parameters:
the list of movie dictionaries
an integer n, indicating the top n genres to print
and prints the most popular n genres, starting with the most popular genre first.
Note: one strategy for writing mostPopularGenres is to first create a dictionary where the keys are the genres and the values are the total number of movies for each genre. After that dictionary is built, then it can be determined what the top n most popular genres are.
>>> mostPopularGenres(movieList,10)
Top 10 most popular movie genres:
33664: Drama
22823: Comedy
11673: Thriller
10169: Romance
9562: Action
9280: Documentary
8566: Horror
7056: (no genres listed)
6913: Crime
5343: Adventure
>>> mostPopularGenres(movieList,20)
Top 20 most popular movie genres:
33664: Drama
22823: Comedy
11673: Thriller
10169: Romance
9562: Action
9280: Documentary
8566: Horror
7056: (no genres listed)
6913: Crime
5343: Adventure
4843: Sci-Fi
4576: Animation
4365: Children
3971: Mystery
3820: Fantasy
2300: War
1687: Western
1057: Musical
354: Film-Noir
195: IMAX
Make sure that your file:
Starts with a comment indicating your name, the date, and any collaborators
Save your python file as username_movies.py, e.g, this is how Saoirse Ronan would name her file: sr112_hw8_movies.py
Each function has a docstring
Each function is tested properly
Submit your python file in gradescope