spider22
spider22

Reputation: 119

list not appending values

I have the following code, I am passing dictionary to this function This function uses omdb (https://github.com/dgilland/omdb.py) python module. The dictionary d2(key contain all movies title) The values I get from omdb, I am trying to create a list which I utilize further in my code. For some reason its only appending the last values

def getdetails(d2):
    pprint.pprint(d2)
    for title_movies in d2.keys():
        #give list as output
        #pprint.pprint(title_movies)
        y=omdb.get(title=title_movies, timeout=5)
        movies_dataL=[]
        if 'title' in y:
            #pprint.pprint(y['imdb_rating'])
            movies_dataL.append(title_movies)
            movies_dataL.append(y['imdb_rating'])
        else:
            print('Movie not found')
            movies_dataL.append(title_movies)
            movies_dataL.append('No IMDB Info Available')
    pprint.pprint(movies_dataL)
    return movies_dataL

Here is how I call the function:

results=getdetails(movies_D)
pprint.pprint(results)

Here is the movies_D dictionary

movies_D={"Murder" : "rot,r", "Subedar Joginder Singh" : "grn,4", "Commando" : "blau,9", "Rambo":"gelb,20"}    

Upvotes: 0

Views: 62

Answers (1)

Prune
Prune

Reputation: 77837

On every loop iteration, you start with a clean list. Move the initialization outside the loop:

movies_dataL=[]

for title_movies in d2.keys():
    #give list as output
    #pprint.pprint(title_movies)
    y=omdb.get(title=title_movies, timeout=5)

Upvotes: 3

Related Questions