What is the efficient way to find the non duplicate items in two list ?

List_finish = [1,2]
List_Raw =[1,2,3,4,5]
List_unfinished = []


for raw in List_Raw:
    if raw not in List_finish:
        List_unfinished.append(raw)

I want to get a List_unfinished with [3,4,5] .

The above for loop is very slow in large list. Any efficient method ?

Posted Apr 20, 2020, 3:36 PM UTC

13
K900_Gets paid to write Python // git{hub,lab}.com/K900•

Convert your lists to sets first, then you can just do set_unfinished = set_raw - set_finish

3
amdforliveOP•

So laconic but fast. Thank you so much

1
baghiq•

set is the standard answer. However, if your lists are sorted, there are more efficient algorithms. One example of them is to traverse both arrays at the same time.

3