Here is a shorter Pythonic code, the notebook can be accessed here.
Python provides some cleaner way to comprehend our code. In this script, I have used two of such techniques.
Technique-1: List comprehension
A list comprehension is nothing but looping through an iterable and producing a list as an output. Here we can include computation and conditionals also. The other technique i.e. Technique-2: Dictionary comprehension which is very similar to this, you can read about it here.
E.g. Code without list comprehension
numbers = []
for i in range(10):
numbers.append(i)
print(numbers)
#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Code using list comprehension
numbers = [i for i in range(10)]
print(numbers)
#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Coming to OPs problem, the get_top250_movies() function returns a list of movies with very few details. The exact parameters it returns can be checked like this. As seen in the output the movie details do not contain genres and other details.
from imdb import IMDb
ia = IMDb()
top250Movies = ia.get_top250_movies()
top250Movies[0].items()
#output:
[('rating', 9.2),
('title', 'The Shawshank Redemption'),
('year', 1994),
('votes', 2222548),
('top 250 rank', 1),
('kind', 'movie'),
('canonical title', 'Shawshank Redemption, The'),
('long imdb title', 'The Shawshank Redemption (1994)'),
('long imdb canonical title', 'Shawshank Redemption, The (1994)'),
('smart canonical title', 'Shawshank Redemption, The'),
('smart long imdb canonical title', 'Shawshank Redemption, The (1994)')]
However, the get_movie() function returns a lot more information about a movie including the Genres.
We combine the two functions to get the genres of the top 20 movies. First we call the get_top250_movies() which returns a list of top 250 movies with fewer details (we are only interested in getting the movieID). Then we call the get_movie() for each movieID from the top movies list and this returns us the Genres.
Program:
from imdb import IMDb
#initialize and get top 250 movies; this list of movies returned only has
#fewer details and doesn't have genres
ia = IMDb()
top250Movies = ia.get_top250_movies()
#TECHNIQUE-1: List comprehension
#get top 20 Movies this way which returns lot of details including genres
top20Movies = [ia.get_movie(movie.movieID) for movie in top250Movies[:20]]
#TECHNIQUE-2: Dictionary comprehension
#expected output as a dictionary of movie titles: movie genres
{movie['title']:movie['genres'] for movie in top20Movies}
Output:
{'12 Angry Men': ['Drama'],
'Fight Club': ['Drama'],
'Forrest Gump': ['Drama', 'Romance'],
'Goodfellas': ['Biography', 'Crime', 'Drama'],
'Inception': ['Action', 'Adventure', 'Sci-Fi', 'Thriller'],
"One Flew Over the Cuckoo's Nest": ['Drama'],
'Pulp Fiction': ['Crime', 'Drama'],
"Schindler's List": ['Biography', 'Drama', 'History'],
'Se7en': ['Crime', 'Drama', 'Mystery', 'Thriller'],
'Seven Samurai': ['Action', 'Adventure', 'Drama'],
'Star Wars: Episode V - The Empire Strikes Back': ['Action',
'Adventure',
'Fantasy',
'Sci-Fi'],
'The Dark Knight': ['Action', 'Crime', 'Drama', 'Thriller'],
'The Godfather': ['Crime', 'Drama'],
'The Godfather: Part II': ['Crime', 'Drama'],
'The Good, the Bad and the Ugly': ['Western'],
'The Lord of the Rings: The Fellowship of the Ring': ['Action',
'Adventure',
'Drama',
'Fantasy'],
'The Lord of the Rings: The Return of the King': ['Adventure',
'Drama',
'Fantasy'],
'The Lord of the Rings: The Two Towers': ['Adventure', 'Drama', 'Fantasy'],
'The Matrix': ['Action', 'Sci-Fi'],
'The Shawshank Redemption': ['Drama']}