I have 2 lists:
images = ['ND_row_id_4026.png',
'ND_row_id_7693.png',
'ND_row_id_5285.png',
'ND_row_id_1045.png',
'ND_row_id_2135.png',
'ND_row_id_8155.png',
'ND_row_id_3135.png']
masks = ['MA_row_id_4026.png',
'MA_row_id_7693.png',
'MA_row_id_5285.png',
'MA_row_id_1045.png',
'MA_row_id_2135.png']
I want to keep the masks files. So, I have 2 folders with images and masks and I want to keep all the masks that are in the masks list and the corresponding images. The images list is bigger than the masks.
So ,I want to keep from the images list:
images = ['ND_row_id_4026.png',
'ND_row_id_7693.png',
'ND_row_id_5285.png',
'ND_row_id_1045.png',
'ND_row_id_2135.png']
>Solution :
It seems (based on your sample data) a simple list comprehension should suffice, just replacing the prefix for comparison:
[im for im in images if im.replace('ND', 'MA') in masks]
Output (for your sample data):
[
'ND_row_id_4026.png',
'ND_row_id_7693.png',
'ND_row_id_5285.png',
'ND_row_id_1045.png',
'ND_row_id_2135.png'
]