Just go through a1 and see if there is a corresponding dictionary in the dictionary you created:
mapping = dict(zip(a, b)) matches = [mapping[value] for value in a1 if value in mapping]
Demo:
>>> a = [1.0, 2.0, 2.1, 3.0, 3.1, 4.2, 5.1, 7.2, 9.2] >>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a1 = [2.1, 3.1, 4.2, 7.2] >>> mapping = dict(zip(a, b)) >>> [mapping[value] for value in a1 if value in mapping] [3, 5, 6, 8]
However, note that you are using floating point numbers. You may not be able to exactly match the values, since floating point numbers are binary approximations to decimal values; for example, the value 2.999999999999999 (15 nines) can be represented by the Python str() function as 3.0 , but not equal to 3.0 :
>>> 2.999999999999999 2.999999999999999 >>> str(2.999999999999999) '3.0' >>> 2.999999999999999 == 3.0 False >>> 2.999999999999999 in mapping False
If your input lists a sorted, you can use the math.isclose() function (or its reverse side) along with the bisect module to maintain efficiency:
import bisect try: from math import isclose except ImportError: def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
This checks up to two values ββfrom a per input value; which is guaranteed to be equal to or lower (at index - 1 ) and the next higher value. For your sample a value 2.999999999999999 is halved by index 3 , between 2.1 and 3.0 . Since isclose(3.0, 2.999999999999999) true, it will still allow you to match this value with 4 in b .