You're changing the list while iterating over it. This is never a good idea. A better idea would be to use a while loop that ran whenever the element is in the list. Then, you could remove the element.
Code:
l1=[int(i) for i in input().split(" ")]
itemToRemove=int(input())
while itemToRemove in l1:
l1.remove(itemToRemove)
print(l1)
If you want to do it with a for loop, iterate over a copy of a list:
l1=[int(i) for i in input().split(" ")]
itemToRemove=int(input())
for i in l1.copy():
if i == itemToRemove:
l1.remove(i)
print(l1)
You can also iterate over the number of times the itemToRemove
is in l1
.
l1=[int(i) for i in input().split(" ")]
itemToRemove=int(input())
for _ in range(l1.count(itemToRemove)):
l1.remove(itemToRemove)
print(l1)