can someone please explain to me why the third item in the list is not removed?
oh.. as I am looking at it:
Is it because when the first item is removed the index changes and moves the 3d item to index [1] in this case, while the for loop continues on with index 2 etc.. ?
I guess that might be it now that I think about it, but I would still like to hear the explanation from someone more experienced that my self. Because I could not figure it out otherwise.
If thats the case what would be a good solution for this to work as I intended?
def alist(x): for i in x: if len(i) != 4: x.remove(i) return x test = alist(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234']) print(test)Thanks
oh.. as I am looking at it:
Is it because when the first item is removed the index changes and moves the 3d item to index [1] in this case, while the for loop continues on with index 2 etc.. ?
I guess that might be it now that I think about it, but I would still like to hear the explanation from someone more experienced that my self. Because I could not figure it out otherwise.
If thats the case what would be a good solution for this to work as I intended?