Function Repository Resource:

AssociatePairs

Source Notebook

Convert a list of pairs into an association

Contributed by: Taliesin Beynon

ResourceFunction["AssociatePairs"][{{key1,val1},{key2,val2},}]

gives the Association <|key1val1,key2val2, |>.

Details and Options

The array must be effectively of n×2 dimensions.
ResourceFunction["AssociatePairs"] does not reorder the array.

Examples

Basic Examples (2) 

Make an Association:

In[1]:=
ResourceFunction["AssociatePairs"][{{a, 1}, {b, 2}, {c, 3}}]
Out[1]=

Make an Association from two lists:

In[2]:=
ResourceFunction["AssociatePairs"][Transpose[{{a, b, c}, Range[3]}]]
Out[2]=

Applications (1) 

The number of moons for each planet in the solar system:

In[3]:=
ResourceFunction["AssociatePairs"][
 PlanetData[#, {"Name", "MoonCount"}] & /@ PlanetData[]]
Out[3]=

Properties and Relations (1) 

Equivalent ways to get an Association:

In[4]:=
Association @@ Rule @@@ {{"1st", "John"}, {"2nd", "Cat"}, {"3rd", "Joe"}}
Out[4]=
In[5]:=
Association[Thread[{"1st", "2nd", "3rd"} -> {"John", "Bill", "Joe"}]]
Out[5]=

Possible Issues (1) 

AssociatePairs does not thread over lists:

In[6]:=
ResourceFunction[
 "AssociatePairs"][{{{r, s}, {t, u}}, {{w, x}, {y, z}}}]
Out[6]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 20 December 2018

License Information