-2

I would like to create in python a script that remove multiple object, inserted by the user, from a list.

I tried this:

list = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"]
print= ("do you want to remove something?")
removed = input()
list.remove(removed)

But while doing this I can't remove more than one element. Is there any way to this but also being able to remove two or more elemets?

2 Answers2

0

Here is a simple list comprehension based solution:

lst = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"]
inp = input("Enter element to remove: ")
if inp:
    lst = [i for i in lst if i!=inp]
print(lst)
Abhinav Mathur
  • 5,837
  • 2
  • 8
  • 22
0

Here's code you can use:

mylist =["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"]

print("do you want to remove something?")
user_input = input()
while user_input != "no":
    mylist.remove(user_input)
    for item in mylist:
        print(item, end=" ")
    print("\nanything else?")
    user_input = input()

The program keeps taking strings for removal from the user until the user says no.

Rweinz
  • 210
  • 2
  • 7