Is there a way to create a two-way association or a bijection between two lists of equal length? I know I can use AssociationThread twice, flipping the arguments, but how can I create a bijective function which associates each element in one list with an element of another, and vice-versa?
1 Answer
A bijective function:
list1 = {1, 2, 3, 4};
list2 = {5, 6, 7, 8};
MapThread[(f[#] = #2; f[#2] = #) &, {list1, list2}]
f[2]
(* Out: 6 *)
And the AssociationThread
solution that you mention:
f = <|AssociationThread[list1 -> list2], AssociationThread[list2 -> list1]|>;
f[2]
(* Out: 6 *)