I want to know which elements of list_1
are in list_2
. But I want to avoid for
loop, because both lists are over 2 million elements long.
This is what I got and works, but is too slow:
list_1 = [0,0,1,2,0,0]
list_2 = [1,2,3,4,5,6]
booleans = []
for i in list_1:
booleans.append(i in list_2)
# booleans = [False, False, True, True, False, False]
# Edit: I need the output as list of ordered booleans
I could probably split the list and use multithreading, but I would prefer simplier solution if possible. I know some functions like sum() use vector operations. I am looking for something similar.
Is there something I could use to make my code more efficient?
Edit:
Solution - credits to @crissal:
I used numpy.isin(list_1, list_2)
.
This is much faster than any other method I found.
CodePudding user response:
You can take advantage of the O(1)
in operator complexity for the set()
function to make your for loop more efficient, so your final algorithm would run in O(n)
time, instead of O(n*n)
:
list_1 = [0,0,1,2,0,0]
list_2 = [1,2,3,4,5,6]
s = set(list_2)
booleans = []
for i in list_1:
booleans.append(i in s)
print(booleans)
If you only want to know the elements, you can use an intersection of sets like that, which will be an efficient solution due to the use of set()
function, already optimized by other Python engineers:
list_1 = [0,0,1,2,0,0]
list_2 = [1,2,3,4,5,6]
print(set(list_1).intersection(set(list_2)))
Output:
{1, 2}
Also, to provide a list format output, you can turn your resulting set into a list with list()
function:
print(list(set(list_1).intersection(set(list_2))))
CodePudding user response:
Big Note: Every function like map
Or numpy.isin
has a loop.
You can use the map
function. If you are not familiar with the
CodePudding user response:
Use set()
to get a list of unique items in each list
list_1 = [0,0,1,2,0,0]
list_2 = [1,2,3,4,5,6]
booleans = []
set_1 = set(list_1)
set_2 = set(list_2)
if(set_1 & set_2):
print(set_1 & set_2)
else:
print("No common elements")
Output:
{1, 2}