list.without()?

Stidolph, David stidolph at origin.ea.com
Tue Nov 16 20:58:45 CET 1999


so ...

def without(source, element):
  while element in source:
    source.remove(element)  

Is that what you are looking for?  It would help if I had read the earlier
posts!  :)

-----Original Message-----
From: Christian Tismer [mailto:tismer at appliedbiometrics.com]
Sent: Tuesday, November 16, 1999 11:15 AM
To: Magnus L. Hetland
Cc: python-list at python.org
Subject: Re: list.without()?




"Magnus L. Hetland" wrote:
> 
> "Mike Fletcher" <mcfletch at vrtelecom.com> writes:
> 
> > Well...
> >
> > def without( source, element):
> >       temp = source[:]
> >       while temp:
> >               try:
> >                       temp.remove( element )
> >               except:
> >                       break
> >       return temp
> 
> Whoa!
> 
> Why on *earth* would you use a while/try/break-combination? IMO a much
> more natural solution would be:
> 
> def without(source, element):
>     result = source[:]
>     if element in result:
>         result.remove(element)
>     return result

But that would be wrong.
"Without" means "there is no such object in it", while list.remove
removes the first occourance of an object.

Mike's implementation appears to be very cheap, since through
the try..except clause, he can combine removal with test
for existance.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home

-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list