# [Tutor] scratching my head - still

Steven D'Aprano steve at pearwood.info
Wed Aug 5 04:46:10 CEST 2015

```On Tue, Aug 04, 2015 at 05:52:15PM -0700, Clayton Kirkwood wrote:
> As seen below (closely), some filenames are not being removed while others
> are, such as in the first stanza, some pdfs are removed, some aren't. In the
> second stanza, Thumbs.db makes it through, but was caught in the first
> stanza. (Thanks for those who have proffered solutions to date!)
> I see no logic in the results. What am I missing???

You are modifying the list of files while iterating over it, which plays
all sorts of hell with the process. Watch this:

py> alist = [1, 2, 3, 4, 5, 6, 7, 8]
py> for n in alist:
...     if n%2 == 0:  # even number
...             alist.remove(n)
...     print(n)
...
1
2
4
6
8
py> print(alist)
[1, 3, 5, 7]

If you pretend to be the Python interpreter, and simulate the process
yourself, you'll see the same thing. Imagine that there is a pointer to
the current item in the list. First time through the loop, it points to
the first item, and you print the value and move on:

[>1, 2, 3, 4, 5, 6, 7, 8]
print 1

The second time through the loop:

[1, >2, 3, 4, 5, 6, 7, 8]
remove the 2, leaves the pointer pointing at three:
[1, >3, 4, 5, 6, 7, 8]
print 2

Third time through the loop, we move on to the next value:

[1, 3, >4, 5, 6, 7, 8]
remove the 4, leaves the pointer pointing at five:
[1, 3, >5, 6, 7, 8]
print 4

and so on.

The lesson here is that you should never modify a list while iterating
over it. Instead, make a copy, and modify the copy.

--
Steve
```