1

I have the following list of files and want only keep the ones, containing a certain string pattern. However my little for loop is not working as expected.
I want only these files which contain the string spontan.

But in the end, there are also files containing bars although testing the condition individually works as expected.

What am I missing?

file_list = ['spontan0_2.h5', 'spontan1_4.h5', 'spontan0.h5', 'spontan1_2.h5', 'bars1.h5', 'bars1_4.h5', 'spontan1.h5', 'bars2_4.h5', 'bars2.h5', 'bars1_2.h5', 'spontan1_3.h5', 'spontan0_2.h5', 'spontan0.h5', 'spontan0_3.h5', 'spontan0-p1_2.h5', 'spontan0-p1.h5', 'bars2_3.h5', 'spontan0-p1_3.h5', 'spontan0_4.h5', 'spontan0_3.h5', 'spontan1_4.h5', 'bars1_3.h5', 'bars2_2.h5', 'spontan0-p1_4.h5']
for file in file_list:
    if not 'spontan' in file:
        file_list.remove(file)


file_list
>>> ['spontan0_2.h5', 'spontan1_4.h5', 'spontan0.h5', 'spontan1_2.h5', 'bars1_4.h5',  
     'spontan1.h5', 'bars2.h5', 'spontan1_3.h5', 'spontan0_2.h5', 'spontan0.h5',   
     'spontan0_3.h5', 'spontan0-p1_2.h5', 'spontan0-p1.h5', 'spontan0-p1_3.h5', 'spontan0_4.h5', 
     'spontan0_3.h5', 'spontan1_4.h5', 'bars2_2.h5', 'spontan0-p1_4.h5']

Theoretically the if-condition is working. Maybe I do something wrong with the remove-statement?

'spontan' in 'bars1_4.h5'
>>> False

EDIT:
I was correctly pointed to this question, explaining, that I modify the list, while iterating through it. This leads to the unexpected behavior.

Here is the fix:

filtered_list = [file for file in file_list if 'spontan' in file]
Helmut
  • 311
  • 1
  • 9

0 Answers0