How to Remove an Element from a List

Difference between del, remove, and pop on lists

The effects of the three different methods to remove an element from a list:

remove removes the first matching value, not a specific index:

>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]

del removes the item at a specific index:

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]

and pop removes the item at a specific index and returns it.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]

Their error modes are different too:

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range

removing elements in list(Python)

If you want to keep the list instead of creating a new one (the answer by Thomas Milox is a good one otherwise), you should iterate backward through the list by index. When you remove an element from a list while iterating forwards through the list you may jump over some elements, not processing them. Going backward ensures that no list element removals move any elements that you may still want to process.

Here is an example of how this may look for your code:

list = [2, 4, 9, 0, 4, 6, 8, 3, 43, 44]
for i in range(len(list) - 1, -1, -1): # start at the last element, go until the first one (index 0 - the last value in the range method will not be reached), go backwards
if list[i] % 2 == 0:
del list[i]

You can read a bit more about removing an element by index instead of by value here.
This is required since you would otherwise mutate the list on the wrong position for duplicate values. It may also be a bit faster, since removeneeds to iterate through the list, searching for the element to remove while del list[i] may look up the element that needs to be removed by index.

Iterating backward through a list is also covered here.

How to remove an element from a list by index

Use del and specify the index of the element you want to delete:

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del a[-1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Also supports slices:

>>> del a[2:4]
>>> a
[0, 1, 4, 5, 6, 7, 8, 9]

Here is the section from the tutorial.

Remove elements from list that is in another list while keeping duplicates

you need a little bit more complex loops:


l1 = ['a', 'b', 'b', 'a', 'c', 'c', 'a']
l2 = ['c', 'b']
l3=[]
for item in l1:
if item in l2:
l2.remove(item)
else:
l3.append(item)
>>>l3
['a', 'b', 'a', 'c', 'a']

Remove element from list and print new list in the same statement

You can create a wrapper function for list remove and call that function instead of calling list.remove directly.

def list_remove(l, elem):
l.remove(elem)
return l

print (list_remove(l, elem))

Disclaimer: I really don't recommend you to do this. But, this is just a method that came to my mind.

I really don't see a problem in writing list.remove() and print in separate statements.

Remove item from one list if NOT in another list?

Assuming playing is a list of strings corresponding to player names:

bedwarsPoints = [i for i in bedwarsPoints if i[0] in playing]

How can i delete elements in a list with remove order?

As @np8 commented, you can remove the elements in descending index order like the following:

lst = [1, 3, 5]
lost = ['the', 'cat', 'thinks', 'you', 'are', 'crazy']

for index in reversed(lst): # descending index order
del lost[index]

print(lost)

which prints

['the', 'thinks', 'are']

UPDATE (Thanks to @wwii for the comment)

If the given lst is not sorted in ascending order, you can do like the following instead:

lst = [3, 1, 5]
lost = ['the', 'cat', 'thinks', 'you', 'are', 'crazy']

for index in sorted(lst, reverse=True): # descending index order
del lost[index]

How to remove all elements in a list that appear more than once

The following will work:

list_1 = [1, 2, 3, 4, 4, 3, 3, 7]
res = [i for i in list_1 if list_1.count(i) == 1]

>>>print(res)
[1, 2, 7]


Related Topics



Leave a reply



Submit