Remove element from a list by key (key) and by value (value)

Asked

Viewed 37,709 times

6

I have the following list

my_list = [1,2,3,4,5,6,7,8,9]

Let’s say I want to remove num 7 by value and/or key and keep:

my_list = [1,2,3,4,5,6,8,9]

How do I do?

3 answers

9


my_list = [1,2,3,4,5,6,7,8,9]
val_remove = 7

Remove by value

my_list.remove(val_remove)

Take into account that in this way:

  • Only removes the first occurrence of the element if there are two or more val_remove (7, in this case) will only remove the first one you find
  • If the value does not exist in the list will trigger an exception, if not capture is this:

Valueerror: list.remove(x): x not in list

To remove only the first occurrence, without being sure if the element exists inside the list:

if(val_remove in my_list):
    my_list.remove(val_remove)

To remove all occurrences of val_remove (7, in this case):

while val_remove in my_list:
    my_list.remove(val_remove)

With filter() and lambda:

If python 2.x, to achieve the same (remove all occurrences) can:

filter(lambda a: a != val_remove, my_list)

If you are python 3.x, to achieve the same (remove all occurrences) you can:

list(filter(lambda a: a != val_remove, my_list))

Remove by key ()

Obviously you have to know the index (key) of the value you want to remove beforehand, in this case it is the 6, so we do:

del my_list[6]

If index (key) there is no exception:

Indexerror: list assignment index out of range

If you are not sure that index (key) exists in my_list may also:

my_list = [i for i in my_list if my_list.index(i) != 6] # caso o index (chave) de i for 6 nao e copiado

Taking into account that this last solution is not well remove, it is more copy the list to the same variable without the value that was in the val_remove (key of the 7, is the 6, in this case)

  • Great explanation. Thank you very much, I was perfectly enlightened

  • You’re welcome @Albertopimenta

3

Simple, to remove elements from a list by its value, just use the method remove:

>>> my_list = [1,2,3,4,5,6,7,8,9]
>>> my_list.remove(7)
>>> my_list
[1, 2, 3, 4, 5, 6, 8, 9]

Now, to remove some element from a list by its index, you can use the method pop:

>>> my_list = [1,2,3,4,5,6,7,8,9]
>>> my_list.pop(6)
7
>>> my_list
[1, 2, 3, 4, 5, 6, 8, 9]

See more about working with python lists on official documentation.

2

removes all desired occurrences 1+x...

def remove_item(my_list,*args):
    deletar = list(args)
    for item in deletar:
        while item in my_list:
            my_list.remove(item)
    return my_list

my_list = [1,2,3,4,5,6,2,7,8,1,1,100,4]
remove_item(my_list,1,4)
#[2, 3, 5, 6, 2, 7, 8, 100]
my_list = [1,2,3,"erro",4,5,6,2,7,8,1,1,100,4,"erro","PYTHON"]
remove_item(my_list,"erro",100,1)
#[2, 3, 5, 6, 2, 7, 8, 'PYTHON']
  • 1

    Nice tarbalho +1. It only changed the items to be removed into the function as a list as well. But it’s just a matter of taste. remove_item(my_list, ["erro",100,1]), that way I could erase deletar = list(args), this line

Browser other questions tagged

You are not signed in. Login or sign up in order to post.