Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > list.clear() missing?!?

Reply
Thread Tools

list.clear() missing?!?

 
 
Ville Vainio
Guest
Posts: n/a
 
      04-11-2006
I tried to clear a list today (which I do rather rarely, considering
that just doing l = [] works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-11-2006
Ville Vainio wrote:

> I tried to clear a list today (which I do rather rarely, considering
> that just doing l = [] works most of the time) and was shocked, SHOCKED
> to notice that there is no clear() method. Dicts have it, sets have it,
> why do lists have to be second class citizens?


because Python already has a perfectly valid way to clear a list,
perhaps ?

del l[:]

(lists are not mappings, so the duck typing argument don't really
apply here.)

</F>



 
Reply With Quote
 
 
 
 
Ville Vainio
Guest
Posts: n/a
 
      04-11-2006
Fredrik Lundh wrote:

> > I tried to clear a list today (which I do rather rarely, considering
> > that just doing l = [] works most of the time) and was shocked, SHOCKED
> > to notice that there is no clear() method. Dicts have it, sets have it,
> > why do lists have to be second class citizens?

>
> because Python already has a perfectly valid way to clear a list,
> perhaps ?
>
> del l[:]


Ok. That's pretty non-obvious but now that I've seen it I'll probably
remember it. I did a stupid "while l: l.pop()" loop myself.

> (lists are not mappings, so the duck typing argument don't really
> apply here.)


I was thinking of list as a "mutable collection", and clear() is
certainly a very natural operation for them.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      04-11-2006
Ville Vainio wrote:
> I tried to clear a list today (which I do rather rarely, considering
> that just doing l = [] works most of the time) and was shocked, SHOCKED
> to notice that there is no clear() method. Dicts have it, sets have it,
> why do lists have to be second class citizens?


This gets brought up all the time (search the archives for your
favorite), but your options are basically (renaming your list to lst for
readability) one of::

del lst[:]

lst[:] = []

or if you don't need to modify the list in place,

lst = []

Personally, I tend to go Fredrik's route and use the first.

If you feel really strongly about this though, you might consider
writing up a PEP. It's been contentious enough that there's not much
chance of getting a change without one.

STeVe
 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      04-11-2006
Em Ter, 2006-04-11 *s 10:42 -0600, Steven Bethard escreveu:
> one of::
>
> del lst[:]
>
> lst[:] = []
>
> or if you don't need to modify the list in place,
>
> lst = []
>
> Personally, I tend to go Fredrik's route and use the first.


I love benchmarks, so as I was testing the options, I saw something very
strange:

$ python2.4 -mtimeit 'x = range(100000); '
100 loops, best of 3: 6.7 msec per loop
$ python2.4 -mtimeit 'x = range(100000); del x[:]'
100 loops, best of 3: 6.35 msec per loop
$ python2.4 -mtimeit 'x = range(100000); x[:] = []'
100 loops, best of 3: 6.36 msec per loop
$ python2.4 -mtimeit 'x = range(100000); del x'
100 loops, best of 3: 6.46 msec per loop

Why the first benchmark is the slowest? I don't get it... could someone
test this, too?

Cheers,

--
Felipe.

 
Reply With Quote
 
Ville Vainio
Guest
Posts: n/a
 
      04-11-2006
Steven Bethard wrote:

> If you feel really strongly about this though, you might consider
> writing up a PEP. It's been contentious enough that there's not much
> chance of getting a change without one.


No strong feelings here, and I'm sure greater minds than me have
already hashed this over sufficiently.

It's just that, when I have an object, and am wondering how I can clear
it, I tend to look what methods it has first and go to google looking
for "idioms" second.

Perhaps "clear" method could be added that raises
PedagogicException("Use del lst[:], stupid!")?

*ducks*

 
Reply With Quote
 
=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=
Guest
Posts: n/a
 
      04-11-2006
Felipe Almeida Lessa wrote:
> I love benchmarks, so as I was testing the options, I saw something very
> strange:
>
> $ python2.4 -mtimeit 'x = range(100000); '
> 100 loops, best of 3: 6.7 msec per loop
> $ python2.4 -mtimeit 'x = range(100000); del x[:]'
> 100 loops, best of 3: 6.35 msec per loop
> $ python2.4 -mtimeit 'x = range(100000); x[:] = []'
> 100 loops, best of 3: 6.36 msec per loop
> $ python2.4 -mtimeit 'x = range(100000); del x'
> 100 loops, best of 3: 6.46 msec per loop
>
> Why the first benchmark is the slowest? I don't get it... could someone
> test this, too?


In the first benchmark, you need space for two lists: the old one and
the new one; the other benchmarks you need only a single block of
memory (*). Concluding from here gets difficult - you would have to study
the malloc implementation to find out whether it works better in one
case over the other. Could also be an issue of processor cache: one
may fit into the cache, but the other may not.

Regards,
Martin

(*) plus, you also need the integer objects twice.
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      04-11-2006
Ville Vainio wrote:
> It's just that, when I have an object, and am wondering how I can clear
> it, I tend to look what methods it has first and go to google looking
> for "idioms" second.


I guess del on a list is not that common, so people tend to not know
that it works on lists (and slices!), too. It's too bad that lists have
a pop() method these days, so people can do x.pop() even if they don't
need the value, instead of doing del x[-1]. I don't think I ever needed
to del a slice except for clearing the entire list (and I don't need to
do that often, either - I just throw the list away).

Regards,
Martin
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      04-11-2006
Steven Bethard wrote:


> lst[:] = []
> lst = []


What's the difference here?
 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      04-11-2006
Em Ter, 2006-04-11 *s 17:56 +0000, John Salerno escreveu:
> Steven Bethard wrote:
>
>
> > lst[:] = []
> > lst = []

>
> What's the difference here?


lst[:] = [] makes the specified slice become []. As we specified ":", it
transforms the entire list into [].

lst = [] assigns the value [] to the variable lst, deleting any previous
one.

This might help:

>>> lst = range(10)
>>> id(lst), lst

(-1210826356, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> lst[:] = []
>>> id(lst), lst

(-1210826356, [])

>>> lst = range(10)
>>> id(lst), lst

(-1210844052, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> lst = []
>>> id(lst), lst

(-1210826420, [])


You see? lst[:] removes all elements from the list that lst refers to,
while lst = [] just creates a new list and discard the only one. The
difference is, for example:

>>> lst = range(3)
>>> x = [lst, lst, lst]
>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
>>> lst[:] = []
>>> x

[[], [], []]

>>> lst = range(3)
>>> x = [lst, lst, lst]
>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
>>> lst = []
>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

HTH,

--
Felipe.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments