It is known that if we have two lists in python l1
and l2
, one can use zip(l1, l2)
in order to create a list of tuples from both l1
and l2
.
Now, lets assume that we have two functions f1
and f2
. I would like to create a list of tuples with elements from l1
and l2
(like the zip
output) such that the output of f1
on the element from l1
will be equal to the output of f2
on the element from l2
. In mathematic form:
In my use case, one can assume that f1
and f2
are both injective functions. But, it is not guaranteed that every element of l1
has a match in l2
and vise versa.
Here's a code that does that:
from itetools import product
tuples_list = [
(e1, e2)
for e1, e2 in product(l1, l2)
if f1(e1) == f2(e2)
]
I wonder if there's a better way of doing it (maybe it's already implemented in a built-in library in a faster/cleaner way).
Any help would be much appreciated.