Skip to content Skip to sidebar Skip to footer

I'd Like To Remove Multiple Elements From A List By User Input

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','

Solution 1:

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.

Solution 2:

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)

Post a Comment for "I'd Like To Remove Multiple Elements From A List By User Input"