Reputation: 113
Is there anyway to remove an item from a list in such a way that I return a new list without the removed element?
For instance,
if I have a list ['a','b','c','d','e']
(called my_list), then I would like a method that does the following my_list.newRemove('a')
= ['b','c','d','e']
.
I want to do this in such a way that I could send the element to be removed and the new list as parameters of a function:
func(element, my_list.newRemove(element))
for example:
func('a', list.newRemove('a'))
which would pass 'a'
AND ['b','c','d','e']
I know I can do this by just passing the element and the entire list to the function, and then creating a new subList in the function without the passed element. However, I feel like it would look better to do it the way I'm asking.
Is this just a fundamentally un-pythonic way of doing this? I learned to program with functional languages, and old habits die hard.
Edit: mistakenly wrote set rather than list
Edit 2: Here is an example of what I ultimately want to do:
myList = ['a','b','c','d']
for element in myList:
function(element, myList.newRemove(element)
Where function does a calculation between the element and the rest of the elements in the list.
Upvotes: 2
Views: 14889
Reputation: 1
My understanding of the question is the following if
>>> ls = [1,2,4,3,5,1]
>>> ls.sort()
is the equivalent to
>>> sorted(ls)
[1,1,2,3,4,5]
with the only difference being that sorted preserves the original list unchanged, and returns a new list, while sort will change the original list and returns None
so what would be the equivalent of remove?
>>> ls = [1,2,4,3,5,1]
>>> ls.remove(1) # returns None
>>> print(ls)
[2,4,3,5,1]
>>> removed(ls, 1) ???
such a function is not built-in but can be created quite easily,
>>> removed = lambda ls, elem: [ x for i,x in enumerate(ls) if i != ls.index(elem) ]
What I see a lot of the answers neglected is that ls.remove() deletes only the first occurrence of an element, notice the second 1 is left unchanged, so actually this will be wrong,
>>> removed = lambda ls, elem: [ x for x in ls if x != elem ] # XX Wrong
Upvotes: 0
Reputation: 19027
The pythonic way of removing an element while copying is using comprehension lists: [x for x in my_list if x != element]
. So there is no need of a function for this. For your example:
my_list = ['a','b','c','d']
for element in my_list:
function(element, [x for x in my_list if x != element])
Upvotes: 1
Reputation: 172
Something like this?
def newListRemove(element, list): return element, filter(lambda x: x != element, list)
list = [1,2,3,4]
print newListRemove(2, list)
print list
returns
(2, [1, 3, 4])
[1, 2, 3, 4]
Upvotes: 1
Reputation: 42
def newListRemove(element, list):
list.remove(element)
return list
list = [1,2,3,4]
print(removeFromList(2, list))
print(list)
This should be what you need.
Upvotes: 0
Reputation: 61
I think this is what you mean, but I'm not sure.
def newRemove(element, x):
x.remove(element)
return element, x
print(newRemove("a", ["a", "b","c", "d"]))
which returns:
('a', ['b', 'c', 'd'])
hope this helps.
There is no built in function to do what you are asking, you'd have to make one.
And just a little pro-tip if you are trying to find a function that you can't remember what is called or don't know if it exists, to this: Type list.
not the list you are trying to edit but literally list.
and hit tab, which should then bring up all of the functions that can edit a list. Most of them are self explanatory, but it is easy to figure out what they do by trial and error.
Hope this helps for all your future coding endeavors
Upvotes: 0