Does that help I'm working on a problem in 'Python for Everyone', chapter 7. The program is meant to take a file and reproduce that file in reverse order of characters. This code makes a list of the characters as they appear, but when I use: , I don't get why you want to sort, you can simply use:
I wish this helpful for you As DYZ said in comments, with reverse indexing, you're changing the indices of items that you have already processed, so the code won't fail when it continues. However, it seems you're lucky with the input, since you're deleting two items at once. If you change the input list to this one, it will still fail with an IndexError.
lst = ['EAST', 'EAST', 'NORTH', 'SOUTH']
num = 0
while num < len(lst) - 1:
if lst[num] == 'NORTH' and lst[num + 1] == 'SOUTH':
del lst[num - 1]
num += 1