0

I am performing a list operation as below in python ,it works fine but the order of elements get sorted ,ideally I want the elements order in LISTC to be the same as in LISTA removing the elements from LISTB?how can I do that?

LISTC = list(set(LISTA) - set(LISTB))

1 Answers1

0

Stop removing the order from your types.

LISTC = [el for el in LISTA if el not in set(LISTB)]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    You should create `set(LISTB)` outside the comprehension. Creating it inside the comprehension recreates it on every iteration, defeating the benefit of using a set in the first place. – user2357112 May 19 '16 at 18:10