Sign Up

Sign In

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask question.

Python: pop from empty list

  • 9

I am using below line in a loop in my code

importer = exporterslist.pop(0)

If exporterslist has no entries or it is null, it returns error: IndexError: pop from empty list. How can I bypass exporterslist with no entries in it?

One idea I can think of is if exporterslist is not null then importer = exporterslist.pop(0)
else get the next entry in the loop.
If the idea is correct, how to code it in python?

Share

1 Answer

  1. You’re on the right track.

    if exporterslist: #if empty_list will evaluate as false.
        importer = exporterslist.pop(0)
    else:
        #Get next entry? Do something else?
    
    • 0
Leave an answer

Leave an answer

Browse

close