Re: Question about idioms for clearing a list
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Question about idioms for clearing a list

From: <bearophileHUGS@lycos.com>
Date: Tue Jan 31 2006 - 22:47:55 CET

Diez B. Roggisch:
> The reason is that l = [] just rebinds a new object (a list, but it could be
> anything) to a name, while l[:] = [] will alter the object _referred_ to by
> l. That is a HUGE difference!

In my programs I have seen that there is another practical difference
between version 1 and 3:
(1) mylist[:] = []
(3) mylist = []
If you create a big mylist again and again many times, the version 1
uses the memory more efficiently (probably less work for the garbage
collector), and the program can be (quite) faster (this is true in some
implementations different from CPython too).

Bye,
bearophile
Received on Tue Feb 7 20:26:03 2006