stupid/style/list question

Raymond Hettinger python at rcn.com
Tue Jan 8 17:40:08 EST 2008


On Jan 8, 7:34 am, "Giampaolo Rodola'" <gne... at gmail.com> wrote:
> I was wondering...
> To flush a list it is better doing "del mylist[:]" or "mylist = []"?
> Is there a preferred way? If yes, why?


To empty an existing list without replacing it, the choices
are "del mylist[:]" and "mylist[:] = []".  Of the two, I
prefer the former because it reads more explicity (delete
all of the items from the list").  In terms of performance,
they are basically the same.

To replace a list, "mylist = []" is the way to go.  This is
an affirmative statement that you are creating a new list.

Raymond



More information about the Python-list mailing list