list.without()?

Rob Hodges s323140 at student.uq.edu.au
Tue Nov 16 03:39:01 EST 1999


mlh at vier.idi.ntnu.no (Magnus L. Hetland) writes:

> Is there a function or method in the standard language that can
> non-destructively return a list without a specified element? Would it
> be possible to add it as a method to lists?
> 
> i.e:
> 
> # pseudocode
> 
> class list:
>     def without(self,element):
>         temp = self[:]
>         temp.remove(element)
>         return temp

Use a lambda with filter, like this:

>>> a = [1,2,3,5,2,1,4,9,1,3]
>>> a
[1, 2, 3, 5, 2, 1, 4, 9, 1, 3]
>>> b = filter(lambda(elt): elt != 1, a)
>>> b
[2, 3, 5, 2, 4, 9, 3]
>>> a
[1, 2, 3, 5, 2, 1, 4, 9, 1, 3]
>>> 

It would be kinda nice to have such a thing as a list method, now that
you mention it, but this is a pretty reasonable one-liner solution.

-Rob





More information about the Python-list mailing list