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
  • 124,992
  • 159
  • 614
  • 958
ariel
  • 795
  • 2
  • 9
  • 11

8 Answers8

53

If you want to modify the list in-place,

a[:] = [x for x in a if x != [1, 1]]
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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
25

Use a list comprehension:

[x for x in a if x != [1, 1]]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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
  • 198,619
  • 38
  • 280
  • 391
  • 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
  • 110,290
  • 27
  • 149
  • 241
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,701
  • 3
  • 15
  • 43
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1
filter([1,1].__ne__,a)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 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,139
  • 1
  • 21
  • 33