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.