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

Terry Reedy tjreedy at udel.edu
Mon Jul 12 07:29:01 CEST 2010

On 7/11/2010 1:58 PM, Diego Jacobi wrote:

> 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.

In CPython, popping copies a reference and them deletes it from the 
list. The item popped is not copied. It is a convenience, which I 
proposed, but not a necessity. You can easily write a function that 
returns a slice after deleting it.

def pop_slice(lis, n):
   tem = lis[:-n]
   del lis[:-n]
   return tem

I expect this to run faster than popping more than a few items one at a 

Terry Jan Reedy

More information about the Python-ideas mailing list