79

Assume you have a list

>>> m = ['a','b','c']

I'd like to make a new list n that has everything except for a given item in m (for example the item 'a'). However, when I use

>>> m.remove('a')
>>> m
m = ['b', 'c']

the original list is mutated (the value 'a' is removed from the original list). Is there a way to get a new list sans-'a' without mutating the original? So I mean that m should still be [ 'a', 'b', 'c' ], and I will get a new list, which has to be [ 'b', 'c' ].

4
  • 2
    What do you want the end result to be? You can copy m into a new list with m[:]
    – Ben
    Jul 28, 2014 at 21:06
  • 1
    You mean... to remove something from a list without changing the list? Or do you mean like replacing an item in a list with some sort of null representation so the indices don't change? Jul 28, 2014 at 21:06
  • yeah, i mean to remove something without changing the list Jul 28, 2014 at 21:11
  • you mean my_var = m[0]? Jul 28, 2014 at 21:24

8 Answers 8

98

I assume you mean that you want to create a new list without a given element, instead of changing the original list. One way is to use a list comprehension:

m = ['a', 'b', 'c']
n = [x for x in m if x != 'a']

n is now a copy of m, but without the 'a' element.

Another way would of course be to copy the list first

m = ['a', 'b', 'c']
n = m[:]
n.remove('a')

If removing a value by index, it is even simpler

n = m[:index] + m[index+1:]
4
  • 4
    In this specific instance I feel it's worth mentioning if you want a copy of the the list except for the 0th element you can slice it with m[1:].
    – kylieCatt
    Jul 28, 2014 at 21:14
  • 2
    True, but I read the question as it is desired to remove an element by value, not by index. Still, a good point!
    – Krumelur
    Jul 28, 2014 at 21:16
  • It's only useful if you know for sure it's the first element in the list you don't want but I think it'll be useful for future readers to illustrate how list slicing works.
    – kylieCatt
    Jul 28, 2014 at 21:17
  • 3
    I would like to add that the first and second approach have different behaviour if the value is not unique in the list. The first would remove all occurrences of the value, whereas the second would only remove the first occurrence.
    – user12205
    Aug 31, 2018 at 15:17
16

There is a simple way to do that using built-in function :filter .

Here is ax example:

a = [1, 2, 3, 4]
b = filter(lambda x: x != 3, a)
2
  • This is golden. Have my upvote. Can't believe I was running list.pop() in a loop to achieve this. My indexes got so messed up.
    – Joker
    Dec 28, 2022 at 13:36
  • The filter approach does not work. remove only removes 1 element, while the filter recipe will remove all elements equal to the given value. if the list is [1,1,1,1,1,1,1,1,1,2], then remove(x,1) will result in [1,1,1,1,1,1,1,1,2] while the filter recipe will result in [2]
    – Jim Newton
    Oct 3 at 10:09
8

If the order is unimportant, you can use set (besides, the removal seems to be fast in sets):

list(set(m) - set(['a']))

This will remove duplicate elements from your original list though

4

We can do it via built-in copy() function for list; However, should assign a new name for the copy;

m = ['a','b','c']
m_copy=m.copy()
m_copy.remove('a')

print (m)

['a', 'b', 'c']

print(m_copy)

['b', 'c']

2

We can do it without using in built remove function and also without creating new list variable

Code:

# List m
m = ['a', 'b', 'c']

# Updated list m, without creating new list variable
m = [x for x in m if x != a]

print(m)

output

>>> ['b', 'c']
1

You can create a new list without the offending element with a list-comprehension. This will preserve the value of the original list.

l = ['a', 'b', 'c']
[s for s in l if s != 'a']
1

Another approach to list comprehension is numpy:

>>> import numpy
>>> a = [1, 2, 3, 4]
>>> list(numpy.remove(a, a.index(3)))
[1, 2, 4]
1
  • This only works for me with numpy.delete(a, a.index(3)) now
    – yuki
    Mar 28, 2022 at 11:57
0

The question is useful as I sometimes have a list that I use throughout my given script but I need to at a certain step to apply a logic on a subset of the list elements. In that case I found it useful to use the same list but only exclude the needed element for that individual step, without the need to create a totally new list with a different name. For this you can use either:

  1. list comprehension: say you have l=['a','b','c'] to exclude b, you can have [x for x in l if x!='b']
  2. set [only if order is unimortant]: list(set(l) - set(['b'])), pay attention here that you pass 'b' as list ['b']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.