Python List Remove Multiple Items
Solution 1:
There are 2 mistakes in your code. The first one is
while a in li == True:
In fact, this check always returns False since li == True
is False.
It should actually be while (a in li) == True:
, or while a in li:
Also, if you are trying to delete only repeated occurrences of a
(i.e. leave the first occurrence of a
in) then list comprehension won't suit your needs. You will have to add an additional check inside your rem()
function that catches the first occurrence of a
and then executes your loop:
defrem(a, li):
list_length = len(li)
i = 0while (li[i] != a) and (i < list_length):
i += 1# skip to the first occurrence of a in li
i += 1# increment i while i < list_length:
if li[i] == a:
del li[i]
print('Updated list: ', li)
list_length -= 1# decrement list length after removing occurrence of aelse:
i += 1
The code snippet above does not cover the edge cases where the list is empty, or the case a
is not in the list. I'll leave those exercises to you.
Solution 2:
Try changing the while condition to while a in li
defrem(a,li):
try:
while a in li:
li.remove(a)
print('Updated list: ',li)
except ValueError:
print(a,' is not located in the list ',li)
L = [1,2,3,45,3,2,3,3,4,5]
rem(2,L)
In general, if you want to remove duplicates from a list then you can use the built-in set
.
Solution 3:
Assuming you want to remove all instances of a
from L
, you could also just use a simple list comprehension:
def rem(a,li):
return [x for x in li if x != a]
L = [1,2,3,45,3,2,3,3,4,5]
print(rem(2,L))
Which Outputs:
[1, 3, 45, 3, 3, 3, 4, 5]
Solution 4:
That would be a better job for a list comprehension.
L[:] = [a for a in L if a not in (2,)]
Assigning to a slice will mutate the list in place.
I'm updating my answer to account for the various interpretations your question allows and also to make it more general by accepting also strings and multiple values to be removed at once.
defremoved(items, original_list, only_duplicates=False, inplace=False):
"""By default removes given items from original_list and returns
a new list. Optionally only removes duplicates of `items` or modifies
given list in place.
"""ifnothasattr(items, '__iter__') orisinstance(items, str):
items = [items]
if only_duplicates:
result = []
for item in original_list:
if item notin items or item notin result:
result.append(item)
else:
result = [item for item in original_list if item notin items]
if inplace:
original_list[:] = result
else:
return result
Docstring extension:
"""
Examples:
---------
>>>li1 = [1, 2, 3, 4, 4, 5, 5]
>>>removed(4, li1)
[1, 2, 3, 5, 5]
>>>removed((4,5), li1)
[1, 2, 3]
>>>removed((4,5), li1, only_duplicates=True)
[1, 2, 3, 4, 5]
# remove all duplicates by passing original_list also to `items`.:
>>>removed(li1, li1, only_duplicates=True)
[1, 2, 3, 4, 5]
# inplace:
>>>removed((4,5), li1, only_duplicates=True, inplace=True)
>>>li1
[1, 2, 3, 4, 5]
>>>li2 =['abc', 'def', 'def', 'ghi', 'ghi']
>>>removed(('def', 'ghi'), li2, only_duplicates=True, inplace=True)
>>>li2
['abc', 'def', 'ghi']
"""
You should be clear about what you really want to do, modify an existing list, or make a new list with the specific items missing. It's important to make that distinction in case you have a second reference pointing to the existing list. If you have, for example...
li1 = [1, 2, 3, 4, 4, 5, 5]
li2 = li1
# then rebind li1 to the new list without the value 4
li1 = removed(4, li1)
# you end up with two separate lists where li2 is still pointing to the # original
li2
# [1, 2, 3, 4, 4, 5, 5]
li1
# [1, 2, 3, 5, 5]
This may or may not be the behaviour you want.
Post a Comment for "Python List Remove Multiple Items"