IndexError: pop from empty list

Discussion in 'Python' started by chris, May 16, 2014.

  1. chris

    chris Guest

    Any ideas about what this might mean?

    Running Debian Wheezy on a RaspBerry Pi and collecting data on a dispatch thread that is reading input on the serial port (connected to xbee series 1).

    It happens every few days but it really chokes the program.

    Thanks for any tips,

    Exception in thread Thread-2:
    Traceback (most recent call last):
    File "/usr/lib/python2.7/", line 552, in __bootstrap_inner
    File "/usr/local/lib/python2.7/dist-packages/xbee/", line 95, in run
    File "/usr/local/lib/python2.7/dist-packages/xbee/", line 400, in wait_read_frame
    return self._split_response(
    File "/usr/local/lib/python2.7/dist-packages/xbee/", line 288, in _split_response
    info[parse_rule[0]] = parse_rule[1](self, info)
    File "/usr/local/lib/python2.7/dist-packages/xbee/", line 117, in <lambda>
    lambda xbee,original: xbee._parse_samples(original['samples'])
    File "/usr/local/lib/python2.7/dist-packages/xbee/", line 357, in _parse_samples
    digital_data_set = (sample_bytes.pop(0) << 8 | sample_bytes.pop(0))
    IndexError: pop from empty list
    chris, May 16, 2014
    1. Advertisements

  2. chris

    Gary Herron Guest

    The error means that sample_bytes is an empty list so calling pop is an

    Or were you asking something deeper, like *why* sample_bytes is an
    empty list?

    Gary Herron
    Gary Herron, May 16, 2014
    1. Advertisements

  3. chris

    chris Guest

    No, that was pretty much what I was looking for. If anyone has an answer to the deeper question, that would be icing on the cake.

    What is interesting is that usually the traceback shows the line of code that I invoke which, deep inside a library I'm using, has generated an error. In this case I don't know which of my commands has spawned the error.

    I can experiment, I suppose, with putting a try/catch around suspected lines of code...


    chris, May 16, 2014
  4. 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, May 16, 2014
  5. chris

    Peter Otten Guest

    It looks like the xbee library is responsible for reading the right amount
    of bytes and then fails to parse them properly.

    So it is possible (even likely I think) that you have run into a bug in the

    A report to the author/maintainer should be in order. Of course it would
    help if you can find a way to reproduce the error. One way to do that is to
    modify the code

    def _parse_samples(self, io_bytes):
    ... # original code of the method
    except IndexError:
    # replace path with something that makes sense on your system
    with open("/path/to/", "wb") as f:

    wait until the error occurs again and then send the contents of the file along with your bug report.
    Peter Otten, May 16, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.