array filter in python?
Question: For example, I have two lists

    
    
     A           = [6, 7, 8, 9, 10, 11, 12]
    subset_of_A  = [6, 9, 12]; # the subset of A
    
    
    the result should be [7, 8, 10, 11]; the remaining elements 
    

Is there a built-in function in python to do this?


Answer: If the order is not important, you should use `set.difference`. However, if
you want to retain order, a simple list comprehension is all it takes.

    
    
    result = [a for a in A if a not in subset_of_A]
    

**EDIT:** As delnan says, performance will be substantially improved if
`subset_of_A` is an actual `set`, since checking for membership in a `set` is
O(1) as compared to O(n) for a list.

    
    
    A = [6, 7, 8, 9, 10, 11, 12]
    subset_of_A = set([6, 9, 12]) # the subset of A
    
    result = [a for a in A if a not in subset_of_A]
    

