I am trying to update a list using for loop, I understand that it does not work but am unsure what is the best way to do it.
l = ['axy','axy','bxy','cxy']
for i in l:
item = 'ax'
l = [s for s in l if not s.startswith(item)]
print l
Output:
['bxy', 'cxy']
['bxy', 'cxy']
['bxy', 'cxy']
['bxy', 'cxy']
Expected Output:
['bxy', 'cxy']
['bxy', 'cxy']
['bxy', 'cxy']
#1 less loop as the list should've been updated to l = ['bxy','cxy'] after the first loop
It tells me that the list gets updated but the iteration does not, is there anyway I can use list comprehension in the loop?
I am using this to remove files, so when the file is removed, I need to update the list so it does not get prompted again.