0

I managed to find a working solution for this with list comprehensions. But I was curious as to why this works this way. Can someone explain?

Code:

def filter_ms_list(ms_list):

trash_suffix = ['C0', 'C1', 'C2', 'C3', 'C4']

temp_list = ms_list

for ms in temp_list:
    for suffix in trash_suffix:
        if ms.endswith(suffix):
            ms_list.remove(ms)
            break
return ms_list

ms_list = ['IJM612C0', 'IJM612C1', 'IJM612C2', 'IJM612C3', 'IJM666', 'IJM667']

Function returns ['IJM612C1', 'IJM612C3', 'IJM666', 'IJM667']

while it should return ['IJM666', 'IJM667']

While debugging I find that after removing 'IJM612C0' it jumps to 'IJM612C2' instead of the second element in the list.

At first I used the same list to remove and iterate, but even with the temp list, as you see, it doesn't work.

Can someone explain this? Thanks!

MrMag
  • 9
  • 1
  • 3

0 Answers0