How to return list elements using specific indexes?

I have a list BB={3, 5, 10} and a list FF = {1/23 (9 + 2 Sqrt[3]), 1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}

Each element of the list FF I can return like this FF[[i]] where i = 1, 2, 3 (for instance FF[[1]] = 1/23 (9 + 2 Sqrt[3]))

But I would like to return each element of the list FF not by index running through the values i=1,2,3 but by using the list BB elements that are related to the list i in the following way: 1 associated with 3, 2 associated with 5 and 3 associated with 10

So I would like to receive the element FF[[1]] like this FF[3] (and for the remaining elements FF[[2]] like this FF[5] and FF[[3]] like this FF[10])

Could you show how this can be done?

ClearAll["Global*"]
BB={3, 5, 10};
FF = {1/23 (9 + 2 Sqrt[3]), 1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};


I think what you need can be achieved simply by using AssociationThread

BB = {3, 5, 10};
{1/23 (9 + 2 Sqrt[3]),
1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}];
(*<|3 -> 1/23 (9 + 2 Sqrt[3]), 5 -> 1/23 (9 - 2 Sqrt[3]),
10 -> -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))|>*)

FF[3]
(*1/23 (9 + 2 Sqrt[3])*)


Let me know if that works for you.

• Might be worth nothing that the resulting FF even supports FF[[1]] (which will return the same as FF[3]) Apr 23, 2023 at 12:10
• @alex, thanks for the answer! Apr 23, 2023 at 12:25
• @LukasLang it seems that OP understands that your suggested syntax works. They wanted to see if they can have a different notation which is more akin to a functional call.
– alex
Apr 23, 2023 at 12:50
• Ah sorry, you were referring to the Association! Yes, indeed. You can still use index notation with Associations. Absolutely! :)
– alex
Apr 23, 2023 at 12:54

You can also do this using straightforward (though somewhat complicated) indexing, rather than using Associations. Define

FFF[n_] := FF[[Position[BB, n][[1, 1]]]];


Then FFF[3] and FFF[5] are as you expect. Position finds the index in BB that you want, and the [[1,1]] strips away a couple of extra levels of { }.

• Thanks for the answer! Apr 23, 2023 at 14:56
BB = {3, 5, 10};
FF = {1/23 (9 + 2 Sqrt[3]),
1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};

ff[n_Integer] := Lookup[lut, n, "NA"];


Test the lookup table:

ff /@ Range[10]


{"NA", "NA", 1/23 (9 + 2 Sqrt[3]), "NA", 1/23 (9 - 2 Sqrt[3]), "NA", "NA", "NA", "NA", -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}

• Thanks for the answer! Apr 23, 2023 at 14:57

It is not possible to define FF as a list and then write FF[1]. This evaluates to: {FF-list}[1] what is nonsense. But what you can do is to use a new name and use "AssociationThread" as already proposed like e.g.:

FF = {1/23 (9 + 2 Sqrt[3]),
1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};
BB = {3, 5, 10};
ind = AssociationThread[BB -> {1, 2, 3}];
FFF[i_] := FF[[ind[i]]]


Now you can e.g. write:

FFF[5]

1/23 (9 - 2 Sqrt[3])

• Thanks for the answer! Apr 23, 2023 at 12:25
• Hi @Daniel, I don't think it is nonsense. I'm sure that if OP knew about associations then they would't have used a list! :)
– alex
Apr 23, 2023 at 12:53

If you just want to extract elements of FF using the values of BB (rather than their indices):

Extract[FF,PositionIndex[BB][3]]
`

$$\frac{1}{23} \left(9+2 \sqrt{3}\right)$$