25

Lets say I have a list a:

a = [[1, 1], [2, 2], [1, 1], [3, 3], [1, 1]]

Is there a function that removes all instances of [1, 1]?

Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
ariel
  • 795
  • 2
  • 9
  • 11

8 Answers8

50

If you want to modify the list in-place,

a[:] = [x for x in a if x != [1, 1]]
Alex Martelli
  • 811,175
  • 162
  • 1,198
  • 1,373
  • 4
    Why set `a[:]` instead of just `a`? – Raj Jan 28 '14 at 06:58
  • 1
    @Raj: `a = [1]; b = a; a = [2]; b == [1]`; `a = [1]; b = a; a[:] = [2]; b == [2]`. – ephemient May 02 '14 at 15:12
  • 7
    @Raj `a` isn't a box that you put objects in - it's a name tag that you tie to objects. `a = ...` ties the name tag to a new object, but `a[:]` replaces all items of the list that `a` is tied to with the contents of the new list. – wizzwizz4 May 12 '17 at 19:45
24

Use a list comprehension:

[x for x in a if x != [1, 1]]
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
9

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)
ephemient
  • 189,938
  • 36
  • 271
  • 385
  • What's wrong with `filter([1,1].__ne__, a)`? – tzot Feb 14 '10 at 21:08
  • 2
    @ΤΖΩΤΖΙΟΥ That works for `list`, but `__ne__` doesn't exist as a method on `int` or some other types in Python 2.x. This is fixed in Python 3.x, but for consistency... – ephemient Feb 15 '10 at 04:05
7
def remAll(L, item):
    answer = []
    for i in L:
        if i!=item:
            answer.append(i)
    return answer
inspectorG4dget
  • 104,525
  • 25
  • 135
  • 234
6

Here is an easier alternative to Alex Martelli's answer:

a = [x for x in a if x != [1,1]]
Shardvexz
  • 61
  • 1
  • 1
  • This behaves differently - it will set the value of the variable but not modify the object. It appears to work, but doesn't modify the list - rather it replaces it. Anything that's using the old list won't get an update. – wizzwizz4 May 12 '17 at 19:46
4
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_all([1,1],a)

Or more general:

def remove_all(elements, list):
    return filter(lambda x: x not in elements, list)

a = remove_all(([1,1],),a)
KetZoomer
  • 2,258
  • 2
  • 11
  • 33
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
1
filter([1,1].__ne__,a)
John La Rooy
  • 281,034
  • 50
  • 354
  • 495
  • I do prefer `functools.partial(operator.ne,[1,1])`, because (at least in Python 2.x) other types (`int`, for example) don't all have `__ne__`. That does appear to be fixed in Python 3, though: all types derive from `object`, which does have `__ne__`. – ephemient Feb 02 '10 at 20:06
-1

pure python no modules version, or no list comp version ( simpler to understand ?)

>>> x = [1, 1, 1, 1, 1, 1, 2, 3, 2]
>>> for item in xrange(x.count(1)):
...     x.remove(1)
...
>>>
>>> x
[2, 3, 2]

can be made into a def pretty easily also

def removeThis(li,this):
    for item in xrange(li.count(this)):
           li.remove(this)
    return li
TehTris
  • 3,059
  • 1
  • 20
  • 33