[Python-ideas] pop multiple elements of a list at once

Diego Jacobi jacobidiego at gmail.com
Sun Jul 11 19:58:43 CEST 2010


Hi.
As recommended here: http://bugs.python.org/issue9218
I am posting this to this list.



I am currently working with buffer in an USB device and pyusb.
So when i read from the buffer of endpoint, i get an array.Array() list.
I handle this chunk of data with a thread to send a receive the
information that i need.
In this thread, i load a list with all the information that is read
from the USB device, and another layer with pop this information from
the threads buffer.

The thing i found is that, to pop a variable chunk of data from this
buffer without copying it and deleting the elements, i have to pop one
element at the time.

    def get_chunk(self, size):
        for x in range(size):
            yield self.recv_buffer.pop()

I guess that it would be improved if i can just pop a defined number
of elements, like this:

pop self.recv_buffer[:-size]
or
self.recv_buffer.pop(,-size)

That would be... "pop from (the last element minus size) to (the last element)"
in that way there is only one memory transaction.
The new list (or maybe a tuple) points to the old memory address and
the recv_buffer is advanced to a one new address. Data is not moved.

Note that i like the idea of using "pop" as the "del" operator for
lists, but i am concient that this would not be backward compatible.

Thanks.
Diego



More information about the Python-ideas mailing list