I have a list
list = {"a","b","c",…}
and I want the association
<| 1 -> "a", 2 -> "b", 3 -> "c", … |>
This seems like the sort of thing there would be a built-in function for, but I haven't been able to find one.
I could do this using
list // AssociationThread[Range @ Length[#], #] &
or
Association@MapIndexed[First[#2] -> #1 &, list]
or
AssociationMap[list[[#]]&, Range @ Length[list]]
but these are all rather unwieldy. Is there a better way?
AssociationThread[Range@Length[list] -> list]
is likely the fastest. $\endgroup$Range@*Length
because I use it so often; that would at least shorten your code. $\endgroup$Range@Length@list
is one character shorter thanRange@*Length@list
$\endgroup$l = {"a", {"b", "c"}}; a = <||>; MapIndexed[AssociateTo[a, #2 -> #1] &, l, {-1}];
$\endgroup$