Reputation: 23
I am trying to get movies by genres from tmdb movie dataset by converting it into json.
There should be multiple entries for a specific genre like 'adventure', but all i get to see is the last record and seems like the previous records are getting overwritten. I have verified this by adding a print statement in the if statement and the details are showing up in the console but somehow getting overwritten.
Any help would be appreciated. Thanks!!
final_list = []
for i in range(1,1000):
if genre_name in data[str(i)]['genres']:
movie_dict["Title"] = data[str(i)]['title']
movie_dict["Language"] = data[str(i)]['original_language']
final_list = [movie_dict]
return final_list
Upvotes: 2
Views: 66
Reputation: 13232
There are two obvious problems. You redefine final_list
in the loop and you return during the first loop.
Fixing that will give you something like this:
def myfunction(data, genre_name):
movie_dict = {}
final_list = []
for i in range(1,1000):
if genre_name in data[str(i)]['genres']:
movie_dict["Title"] = data[str(i)]['title']
movie_dict["Language"] = data[str(i)]['original_language']
final_list.append(movie_dict)
return final_list
Now there is another, more subtle problem. You always add the same dictionary to the list.
To give an example:
d = {}
l = []
for i in range(5):
d['x'] = i ** 2
l.append(d)
print(l)
Now l
contains the same dictionary 5 times and this dictionary will have the content from the last iteration of the loop: [{'x': 16}, {'x': 16}, {'x': 16}, {'x': 16}, {'x': 16}]
. To fix this you have to create the dictionary in the loop, so in your code you have to move movie_dict = {}
to an appropriate place:
def myfunction(data, genre_name):
final_list = []
for i in range(1,1000):
if genre_name in data[str(i)]['genres']:
movie_dict = {}
movie_dict["Title"] = data[str(i)]['title']
movie_dict["Language"] = data[str(i)]['original_language']
final_list.append(movie_dict)
return final_list
Now some more advanced stuff. I assume data
is a dictionary since you use a string for indexing. If you're not limited to 1000 entries but want to access all the values in the dictionary you can loop over the dictionaries values:
def myfunction(data, genre_name):
final_list = []
for entry in data.values():
if genre_name in entry['genres']:
movie_dict = {}
movie_dict["Title"] = entry['title']
movie_dict["Language"] = entry['original_language']
final_list.append(movie_dict)
return final_list
Now let us create the dictionary on the fly in the call to append
.
def myfunction(data, genre_name):
final_list = []
for entry in data.values():
if genre_name in entry['genres']:
final_list.append({'Title': entry['title'], 'Language': entry['original_language']})
return final_list
This can be rewritten as a list comprehension:
def myfunction(data, genre_name):
return [{'Title': entry['title'], 'Language': entry['original_language']} for entry in data.values() if genre_name in entry['genres']]
That's all.
Upvotes: 2
Reputation: 11
Bro you were actually actually returning the list after each value was added try this way and it will not overwrite it. The main reason is that you were returning the list in the for loop and everytime the loop run it save new dictionary in the list and remove the old one
final_list = []
for i in range(1,1000):
if genre_name in data[str(i)]['genres']:
movie_dict["Title"] = data[str(i)]['title']
movie_dict["Language"] = data[str(i)]['original_language']
final_list.append(movie_dict)
return final_list
Upvotes: 0