Don’t do like the another answer (which has been deleted), because removing elements from a list in the same loop that iterates on it does not work in all cases. Ex:
array = ['sim', 'sim', 'sim', 'sim', 'sim2', 'sim', 'sim3', 'sim4']
for i in array:
array.remove('sim')
print(array) # ['sim2', 'sim', 'sim3', 'sim4']
Note that not all "yes" have been removed. This behavior is best explained here and here.
Finally, the documentation cites two solutions to avoid this problem. Or you create another list with only the values you want:
array = ['sim', 'sim', 'sim', 'sim', 'sim2', 'sim', 'sim3', 'sim4']
outra = []
for x in array:
if x != 'sim':
outra.append(x)
print(outra)
Or you iterate over a copy of the list:
array = ['sim', 'sim', 'sim', 'sim', 'sim2', 'sim', 'sim3', 'sim4']
for x in array.copy():
if x == 'sim':
array.remove(x)
print(array)
The first option above can also be done with comprehensilist on, much more succinct and pythonic:
array = ['sim', 'sim', 'sim', 'sim', 'sim2', 'sim', 'sim3', 'sim4']
outra = [ x for x in array if x != 'sim' ]
print(outra)
Could add that naughty solution with list comprehession to stay top xD
– Woss
I blacked out because as you said was wrong, I’ll give a study and try to remake the correct way, thanks for the observation :)
– Codigo de Senior
@Woss I was going to put but I forgot. It’s there, thank you! :-)
– hkotsubo