faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

list.without() (Removing element from list)

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 288, Python Snippet Support Team


"""
Packages: basic_datatypes.lists
"""
""" 
> 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
Hmmz... Why a method?
"""
def without(lst, elem):
    return filter(lambda x, elem=elem: x != elem, lst)