I am trying to remove items from a list as long as they contain any numeric characters in them. The code I have works, at least it detects them, but at the time of deleting it leaves me one of them, could you tell me my mistake? Thanks in advance
a=["symbol","ETHB3TC","price","0.06715900","s1ymbol","LTCBTC","price","0.00266700"]
def has_numbers(inputString):
return any(char.isdigit() for char in inputString)
for i in a:
eck=has_numbers(i)
print(eck)
If I run it like this, it detects all the elements with numbers without any problem
exit:
False
True
False
True
True
False
False
True
but if I tell it to remove the items, it doesn't remove all of them:
for i in a:
eck=has_numbers(i)
if eck:
a.remove(i)
print(a)
exit:
['symbol', 'price', 's1ymbol', 'LTCBTC', 'price']
I don't understand why 's1ymbol' doesn't remove me, what breaks my brain the most is that I repeat the for loop if it removes it
EDIT: the answer to my question is below, but if you don't want to create a new list you can work in reverse and with the indexes like this:
for i in range(len(a)-1,-1,-1):
eck=has_numbers(a[i])
if eck:
del a[i]