-2

Suppose I have got a list of result set of string as below :

[ B397 B406 B431 B434 B468 B820 B85 ]

I have another list as below :

[ B397 B406 B431 ]

If I want to filter out of result set using list B, then return result set as :

[ B434 B468 B820 B85 ]

How can I filter out?

workings :

 new_set = []

    for item in result_set:
            // item found in another list 
                new_set.append(item)
Alec
  • 8,529
  • 8
  • 37
  • 63
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141

1 Answers1

1

Use a simple list comprehension:

new_list = [x for x in old_list if x not in filter]

You can make the filter faster (O(n) instead of O(n²)) by making filter a set

Alec
  • 8,529
  • 8
  • 37
  • 63