Pop a list from beginning ? and memory saving...

Peter Hansen peter at engcorp.com
Tue Jun 4 23:34:44 EDT 2002


Terry Reedy wrote:
> 
> "Peter Hansen" <peter at engcorp.com> wrote in message
> news:3CFD5B60.90831F21 at engcorp.com...
> > >     self._v[:] = []
> > I can't parse that.  What does it do?
> 
> Try the wonerful interactive interpreter:
> 
> >>> l = range(10)
> >>> l
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> l[3:7] = []
> >>> l
> [0, 1, 2, 7, 8, 9]
> >>> l[:] = []
> >>> l
> []

Okay, so it's modifying the list in place to be empty, 
equivalent to the del self._v[:] thing someone else posted?

I must have missed the _why_ part...  ah!  enlightenment:
you are removing the contents of the list, which might
have other references to it, rather than rebinding the name
self._v to a new empty list but leaving those other 
references alone.

Still have to think about times when that would be desirable,
but thanks for the nudge. :)

-Peter



More information about the Python-list mailing list