python needs leaning stuff from other language

Paul McGuire ptmcg at
Sat Apr 4 16:40:02 CEST 2009

On Apr 3, 11:48 pm, Tim Wintle < at> wrote:
> del mylist[:]
> * or *
> mylist[:] = []
> * or *
> mylist = []
> which, although semantically similar are different as far as the
> interpreter are concerned (since two of them create a new list):

Only the last item creates a new list of any consequence.  The first
two retain the original list and delete or discard the items in it.  A
temporary list gets created in the 2nd option, and is then used to
assign new contents to mylist's [:] slice - so yes, technically, a new
list *is* created in the case of this option. But mylist does not get
bound to it as in the 3rd case.  In case 2, mylist's binding is
unchanged, and the temporary list gets GC'ed almost immediately.

-- Paul

More information about the Python-list mailing list