Debugging List Traversals

The program below keeps a playlist as a list of track lengths in seconds, and skip holds the length of one track the listener has marked. It is meant to remove that track and every track under two minutes, then count where a track is shorter than the one before it. Run as written, it stops with an error, and each repair uncovers the next problem. Run the program to see the error. As the lesson finds each problem, repair it in the program and run it again.
tracks = [185, 94, 102, 240, 215, 131, 176, 158]
skip = 200
tracks.remove(skip)
for t in tracks:
    if t < 120:
        tracks.remove(t)
drops = 0
for i in range(len(tracks)):
    if tracks[i + 1] < tracks[i]:
        drops += 1
print(tracks, drops)
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

The skipped track

Run as written, the program stops at line 3. The last line of its error message is
ValueError: list.remove(x): x not in list
The marked track is 200 seconds long, and no track on this playlist is. A marked track that isn't on the playlist leaves nothing to remove, and the program should carry on.
Line 3 needs remove rather than pop, since the program knows the track's length but not its position. The repair checks that the track is there before removing it:
if skip in tracks:
tracks.remove(skip)
With skip at 200 the test is False, so the playlist keeps all eight tracks. On a playlist that does hold a 200-second track, remove deletes the first one it finds.

Two uses of in

The word in now does two different jobs in the program. In the test skip in tracks, it works as an operator. Python searches the list for 200 and answers True or False without changing it. Here print(skip in tracks) prints False, and print(215 in tracks) prints True. In for t in tracks: the word belongs to the loop header, which walks the list by position and gives t the track at each position in turn.

Removing inside a loop

With line 3 repaired, a print(tracks) placed right after the first loop shows
[185, 102, 240, 215, 131, 176, 158]
The 102-second track survived, although 102 is less than 120. The for loop keeps its own position in the list. At position 0, t is 185, which stays. At position 1, t is 94, and remove deletes it. Every later track shifts one place to the left, so 102 now sits at position 1. The loop goes on to position 2, where it finds 240, and t never takes the value 102.
The repair builds a new list of the tracks to keep, so its test flips to t >= 120:
long_tracks = []
for t in tracks:
if t >= 120:
long_tracks.append(t)
tracks = long_tracks
The loop reads tracks and never changes it, so every track gets its turn, and the playlist becomes [185, 240, 215, 131, 176, 158]. Looping over a copy of the list while removing from the original would also work.

Comparing neighbors

The last loop compares each track with the one after it. On the six remaining tracks it stops with
IndexError: list index out of range
The list has positions 0 through 5, and range(len(tracks)) gives i every one of them. When i is 5, tracks[i + 1] asks for position 6, which doesn't exist. The last track has no track after it, so it can't start a pair. Looping over every position is the usual slip, since n items make only n − 1 neighboring pairs.
For the six tracks, the header becomes for i in range(len(tracks) - 1):, and i runs from 0 to 4. The repaired program prints [185, 240, 215, 131, 176, 158] 3. The three drops are from 240 to 215, from 215 to 131, and from 176 to 158.