IndexError: pop from empty list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri May 16 02:41:22 EDT 2014


On Thu, 15 May 2014 21:36:49 -0700, chris wrote:

> Any ideas about what this might mean?
[jumping ahead]
>     digital_data_set = (sample_bytes.pop(0) << 8 | sample_bytes.pop(0))
> IndexError: pop from empty list

sample_bytes is an empty list. Or it could be a list with just a single 
sample. You try to pop from it twice in a row, so unless it contains at 
least two items, one or the other of the pops will fail.

Without understanding the context, it's impossible to say what the best 
way to fix this, but my guess is that you need to guard this clause with 
something that ensures that there are at least two samples, and if not, 
just waits until there are. E.g.:

if len(sample_bytes) >= 2:
    digital_data_set = (sample_bytes.pop(0) << 8 | sample_bytes.pop(0))


or similar. This assumes the function will try again a moment later, 
after it's had a chance to gather some more data from the serial port. 
Ultimately, that's the problem: you're trying to process data faster than 
in can be collected.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list