0

I have two lists and I want to process only the elements part of which do not match.

ListA = ['CAT.txt','CAT.txt.ext','DOG.txt','DOG.txt.ext','TIGER.txt.ext',TIGER.txt'] ListB = ['CAT_NEW.txt','CAT_NEW.txt.ext', 'TIGER_NEW.txt', 'TIGER_NEW.txt.ext']

List B is a subset of list A with "_NEW"

Output I want: ListC = ['DOG.txt', 'DOG.txt.ext']

This is a work around for another question I had asked : Python: Trying to check if file exists and if not create new files and final output list . Checked multiple threads but the .txt.ext is hard to split on...

Figured if I split the input lists, I can implement without checking for the log file.

I checked this : Python: how to find the element in a list which match part of the name of the element

Ram
  • 547
  • 2
  • 4
  • 13

1 Answers1

1

Remove the strings '_NEW' from the ListB, and use set operation:

In [1]: ListA = ['CAT.txt','CAT.txt.ext','DOG.txt','DOG.txt.ext','TIGER.txt.ext','TIGER.txt']                                 

In [2]: ListB = ['CAT_NEW.txt','CAT_NEW.txt.ext', 'TIGER_NEW.txt', 'TIGER_NEW.txt.ext']                                       

In [3]: lb= [ s.replace("_NEW","") for s in ListB ]                                                                           

In [4]: lb                                                                                                                    
Out[4]: ['CAT.txt', 'CAT.txt.ext', 'TIGER.txt', 'TIGER.txt.ext']

In [5]: list(set(ListA)-set(lb))                                                                                              
Out[5]: ['DOG.txt', 'DOG.txt.ext']
kantal
  • 2,331
  • 2
  • 8
  • 15