-1

I have two lists, list1 and list2, I want to get all elements that are present in list1 but not in list2. Currently, I am typecasting lists into set and subtracting them and then again typecasting result to list.

list(set(list1) - set(list2));

But this is not performance efficient. Can you please suggest me alternate ways to do the same thing?

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80

1 Answers1

0

Using a list comprehension is probably simplest:

[x for x in list1 if not x in list2]
C. Feenstra
  • 593
  • 3
  • 11