Re: Question about idioms for clearing a list



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

.



Relevant Pages

  • Re: Gospels are not eye witness accounts
    ... BernardZ wrote: ... The practical difference between this claim and "anything I don't want ... Roger Pearse ... Prev by Date: ...
    (soc.history.ancient)
  • Re: There is no Delphi Marketing manager.
    ... there is no practical difference. ... > regardless of 'cannot' or 'will not'. ... I'm not saying Borland's choices are not good/correct choices in this ... Prev by Date: ...
    (borland.public.delphi.non-technical)