how to delete element the list in a for loop?
the variable a should not be reassign .modify it but don’t make new one.
the code below doesn’t work properly
a = [1,2,2,3,2,1,4]
i = 0
for j in a:
print(j)
if(j<=2):
print('deleting ',i)
del a[i]
else:
i+=1
print(a) #[3, 2, 1, 4]
use filter…
or list comprehension
\edited to not destroy the original pointer… although im not sure why… seems like less than perfect design