lets say I have a=[[1,1],[2,2],[1,1],[3,3],[1,1]] is there a function that remove all instances of [1,1]
thanks
lets say I have a=[[1,1],[2,2],[1,1],[3,3],[1,1]] is there a function that remove all instances of [1,1]
thanks
Use a list comprehension:
[x for x in a if x != [1, 1]]
If you want to modify the list in-place,
a[:] = [x for x in a if x != [1, 1]]
new_list = filter(lambda x: x != [1,1], a)
Or as a function:
def remove_all(element, list):
return filter(lambda x: x != element, list)
a = remove([1,1],a)
Or more general:
def remove_all(elements, list):
return filter(lambda x: x not in elements, list)
a = remove(([1,1],),a)
def remAll(L, item):
answer = []
for i in L:
if i!=item:
answer.append(i)
return answer
Google finds Delete all items in the list, which includes gems such as
from functools import partial
from operator import ne
a = filter(partial(ne, [1, 1]), a)