1

So I am trying to use the IMDBpy module to connect a list of programs to their respective Genres. First I am trying to get a new list of their IMDB IDs to, from there, get their corresponding Genre. Unfortunately I am unable to iterate through my list of programs and generate a new list of corresponding IDs. Somehow my function gets stuck at the first iteration. What am I missing in my definition?

from imdb import IMDB
ia = IMDb()
programs = ['midsomer murders', 'wheeler dealers: dream car', 'solo: a star wars story (2018)']
def ids_list(x):
    ids = []
    for item in list(x):
        movie = ia.search_movie(item)[0].getID()
        for movie in movies:
            ids.append(movie)
        return ids
    
ids_list(programs)

output:

['0118401']

As you can see only the first item comes through, whereas my code suggests it should append every item in the list after running it through ia.search_movie(item)[0].getID(). Thanks in advance!

5
  • 1
    move the return ids to out of the outer-for-loop n try Commented Jul 21, 2020 at 9:48
  • i mean just un-indent the return statement once Commented Jul 21, 2020 at 9:49
  • Yes just done that, now I get an IndexError: list index out of range Commented Jul 21, 2020 at 9:50
  • that would be on this line - ia.search_movie(item)[0] - sometimes you might not be getting any elements , so when you try to access 0-th element it throws error Commented Jul 21, 2020 at 9:53
  • 1
    use if len(ia.search_movie(item)): like so before that line Commented Jul 21, 2020 at 9:54

2 Answers 2

1

Your return is indented incorrectly. It returns after the first iteration of the for loop:

from imdb import IMDb
ia = IMDb()
programs = ['midsomer murders', 'wheeler dealers: dream car', 'solo: a star wars story (2018)']
def ids_list(x):
    ids = []
    for item in list(x):
        movie = ia.search_movie(item)
        if movie:
          ids.append(movie[0].getID())
        else:
          ids.append(None)
    return ids
    
print(ids_list(programs))

shortened version using list comprehension and assignment operator (Python 3.8+):

from imdb import IMDb
ia = IMDb()
programs = ['midsomer murders', 'wheeler dealers: dream car', 'solo: a star wars story (2018)']
def ids_list(x):
    return [m[0].getID() if (m:=ia.search_movie(i)) else None for i in x]
    
print(ids_list(programs))
Sign up to request clarification or add additional context in comments.

6 Comments

Again syntax error in your code in (m:=ia.search_movie(i))
shortened version requires python 3.8+. I'm guessing you are using an older version. You can use the top version
Yes true I am, the top version is also giving me TypeError: quote_from_bytes() expected bytes I think it has to do with the search movie value not being a string.
That would be a problem with the programs list, containing data you haven't shown. You should check the output in list programs. You could cast it to a string, but the real problem is your original list is malformed and needs to be fixed and the reason you are getting something other than strings found.
also, are you sure you only want the first movie's ID found and not all the possible IDs found?
|
1

I mean change your for-loop code to this :

for item in list(x):
    if len(ia.search_movie(item)):
        movies = ia.search_movie(item)[0].getID()
        [ids.append(movie) for movie in movies]
return ids

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.