I know in Python, if I have:
list_1 = [1,2,3]
list_2 = [2,3,4]
I can do the following to find the intersection between them:
list(set(list_1) & set(list_2))
But there is one problem with this approach: sets do not maintain order, as lists do. Therefore, if I have:
list_1 = [3,2,1]
list_2 = [2,3,4]
I get:
list(set(list_1) & set(list_2))
although I would prefer to have order from the first list, i.e.:
Is there an alternative intersection method that preserves the resulting “intersection set” in the same order as the first list?
source
share