-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
  • 7,110
  • 7
  • 28
  • 53
Raju yourPepe
  • 2,859
  • 15
  • 63
  • 131

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
  • 7,110
  • 7
  • 28
  • 53