Skip to content
Advertisement

How can I remove two or more objects of a list from a single input in Python?

This is my sample database:

Sample database copy.png

In my script now, I am adding elements to the list with the following code:

JavaScript

So now if type “2 JO” my list will have two new elements “John” and “John”.

Now I want to do exactly the same but for eliminating objects of a list. I tried to replace the current operator “+=” for “-=” but surprisingly it is not working. Any ideas on how I could solve this?

PS. I need to do it all out of the same input. I cannot ask separately for key and quantity. I want to make a replication of the above code but for removing the objects.

Advertisement

Answer

You could use list.remove():

Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

JavaScript

Example Usage:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement