I want to iterate over a list of files in Python 3. They are CSV files containing matrices. I want to do the same with all of them so I wanted to create a list with their names, remove all other files in the folder from the list and do my transformation with the relevants.

My target files all end with "2m.csv" (e.g.: 14-17\_CCK\_all\_2m.csv) and my results at the end of the process will end with "1m.csv". Still when I run the following script in Jupyter notebook, the result contains some files ending with "1m.csv" (they remained there from an earlier cycle of the development)

```
import os
myfiles = os.listdir()

for item in myfiles:
    if item[-6:] != "2m.csv":
        myfiles.remove(item)

```

Interestingly, if I test one of the false negatives in a separate lines, I get a True answer, so the if statement should have eliminated it from my list in the script above - which it did with some of them, but not with some others:

```
myfiles[1][-6:] != "2m.csv"
>>> True

```

All the files in question have very similar name structure. Thanks for your help.