Search code examples
pythonlistpycharm

removing an item from a list of strings


I have a list of strings stored in a variable pdf paths, and I have another list of strings that if any item in my pdf paths list contains, it should be removed.

It works for some strings but not for others even though the string contain the same word.

def remove_derivaties(blacklist, pdf_list):
    for x in blacklist:
        for y in pdf_list:
            if x in y:
                pdf_list.remove(y)
    return pdf_list


def main():
    string = ["P:\\Example Files\\Latest\\PRM-FRM-011 Calc Review Form.pdf",
              "P:\\Example Files\\Latest\\PRM-FRM-003 Project Assignment Form.pdf",
              "control_string"]

    exclude = ["P:\\Example Files",
               "control_string1"]

    not_excluded = remove_derivaties(exclude, string)
    print(not_excluded)

The output is ['P:\\Example Files\\Latest\\PRM-FRM-003 Project Assignment Form.pdf', 'control_string']

yet the exclude word is contained in both strings


Solution

  • Is this the correct behaviour?

    from typing import List
    
    string = ["P:\\Example Files\\Latest\\PRM-FRM-011 Calc Review Form.pdf",
              "P:\\Example Files\\Latest\\PRM-FRM-003 Project Assignment Form.pdf",
              "control_string"]
    
    exclude = ["P:\\Example Files",
               "control_string1"]
    
    def substring_in_string(substring: str, string: str) -> bool:
        return substring in string
    
    def substrings_in_string(substrings: List[str], string: str) -> List[bool]:
        return [substring_in_string(substring, string) for substring in substrings]
    
    not_excluded = [s for s in string if not any(substrings_in_string(exclude, s))]
    
    output:
    
    ['control_string']