list.without()?

Christian Tismer tismer at appliedbiometrics.com
Tue Nov 16 12:14:44 EST 1999


"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




More information about the Python-list mailing list