I'm familiar with list.remove
, which will mutate a list by deleting the first instance of the passed element, raising a value error if the element is not present.
What I'm looking for is a function that does not mutate the list, but rather returns a copy of the list without the given element. So, for instance, instead of executing
l = [1,2,3]
l.remove(2)
I would execute
l = [1,2,3].remove(2)
The purpose of this is to make it a bit simpler to put this type of logic into a dict definition. So that rather than:
l = [1,2,3]
l.remove(2)
d = {
'theList': l
}
I just have
d = {
'theList': [1,2,3].remove(2)
}
Does such a function exist?
Note that I want to remove only the first instance of the value in question, not all instances, so [x for x in [1,2,3] if x != 2]
does not do what I want. Though I'm not sure if there is a list comprehension way to do this.