In pure Python, you can use a generator comprehension with next and enumerate:
A = [53, 1, 17, 4, 13, 2, 17] B = [4, 3, 1] B_set = set(B) first_lst = next(idx for idx, val in enumerate(A) if val in B_set) # 1
Note we hash values in B via set to optimise lookup cost. Complexity is O(m + n), where m and n are number of elements in A and B respectively. To error handle in case no match is found, you can supply a default argument:
first_list = next((idx for idx, val in enumerate(A) if val in B_set), len(A))
If you are happy to use a 3rd party library, you can use NumPy. No error handling here in case of no match:
import numpy as np A = np.array([53, 1, 17, 4, 13, 2, 17]) B = np.array([4, 3, 1]) first_np = np.where(np.in1d(A, B))[0][0] # 1
index = [ListB.index(j) for j in ListA if j in ListB][0]np.in1d()could be useful: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…ListAorListB?