Nov-11-2019, 01:20 AM
A while loop that starts at the END of the list and works backward will avoid the skipping problem and is considered "safer", kind of like bungie jumping is safer than cliff diving. Also, you can just remove items using that remove function but I think that would miss the point the instructor is trying to make.
lst = [1,2,3,4,5,6,7] start = len(lst)-1 while start > -1 : if lst[start] == 4 : lst.remove(lst[start]) start -= 1 print (lst) lst = [1,2,3,4,5,6,7] lst.remove(3) print(lst)
Output:[1, 2, 3, 5, 6, 7]
[1, 2, 4, 5, 6, 7]