Python: pop from empty list
I am using below line in a loop in my code
importer = exporterslist.pop(0)
If the exporter list has no entries or this null
, it returns error: IndexError: pop from empty list
. How can I bypass the exporter list without entries in it?
One idea I can think of is if the exporter list is nonzero then importer = exporterslist.pop(0)
otherwise get the next entry in the loop. If the idea is correct, how do I code it in python?
source to share
This...
exporterslist.pop(0) if exporterslist else False
.. is somewhat the same as @nightshadequeen's accepted answer just shorter:
>>> exporterslist = []
>>> exporterslist.pop(0) if exporterslist else False
False
or maybe you can use this to get no return:
exporterslist.pop(0) if exporterslist else None
>>> exporterslist = []
>>> exporterslist.pop(0) if exporterslist else None
>>>
source to share