I am trying to iterate the list and delete the last element based on the condition. But I am getting errors.
x = [0,2,1]
for y, v in enumerate(x):
if v is 1:
del x[y]
print(x)
output:
[0,2,1]
I am trying to iterate the list and delete the last element based on the condition. But I am getting errors.
x = [0,2,1]
for y, v in enumerate(x):
if v is 1:
del x[y]
print(x)
output:
[0,2,1]
It's not recommended to iterate into a list changing its elements, since you are removing the element by value you can do like this:
x = [0, 2, 1]
for i in range(x.count(1)):
x.remove(1)
print(x)
Output
[0, 2]