Better solution

Ken Seehof kseehof at neuralintegrator.com
Tue Aug 20 16:29:18 EDT 2002


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of holger krekel
> Sent: Tuesday, August 20, 2002 12:37 PM
> To: Michael Hudson
> Cc: python-list at python.org
> Subject: Re: Better solution
> 
> 
> Michael Hudson wrote:
> > holger krekel <pyth at devel.trillke.net> writes:
> > 
> > > > If you want to mutate the list, I'd say:
> > > > 
> > > > lst[:] = filter(None, lst)
> > > > 
> > > > is better than the monstrosity above.
> > > 
> > > why the '[:]'? 
> > > 
> > > doesn't seem to make any difference here unless
> > > filter is allowed to return the same lst-object.
> > 
> > It depends whether you want to change the acutal list object or merely
> > have 'lst' refer to a changed list.  Bo was using .pop in his
> > question, so it's possible he wanted the former.
> 
> Aehem. i must be missing something obvious. 
> 
> Yes, append/sort/pop and friends are inplace-methods.
> 
> But isn't 
> 
>     filter(None,lst) 
> 
> supposed to return a new list? 
> 
> somebody is missing something here :-)
> 
>     holger

Yes, but that brand new list object is being copied into the target list,
so the target list doesn't change it's identity.

>>> a = b = [1,2,3]
>>> b[:] = [4,5,6]
>>> a
[4, 5, 6]

>>> a = b = [1,2,3]
>>> b = [4,5,6]
>>> a
[1, 2, 3]

- Ken






More information about the Python-list mailing list