Wolfram Research

Function Repository Resource:

ToAssociations

Source Notebook

Recursively replace lists of rules with associations

Contributed by: Taliesin Beynon (Wolfram Research)

ResourceFunction["ToAssociations"][{key1val1,key2val2,}]

recursively replaces lists of rules with associations having the same keys and values.

Details and Options

ResourceFunction["ToAssociations"] works with associations as well as lists of rules.

Examples

Basic Examples

Convert a list of rules to an Association:

In[1]:=
ResourceFunction["ToAssociations"][{a -> 1, b -> 2}]
Out[1]=

Convert a nested list of rules:

In[2]:=
ResourceFunction["ToAssociations"][{a -> 1, b -> {c -> 2}}]
Out[2]=

Convert a mix of associations and rule lists:

In[3]:=
ResourceFunction["ToAssociations"][<|a -> 1, b -> {c -> 2}|>]
Out[3]=

Scope

Convert a mix of lists and associations:

In[4]:=
ResourceFunction[
 "ToAssociations"][{a -> 1, b -> {c -> 2}, d -> <|e -> {f -> 3}|>}]
Out[4]=

List items that are not in the form {keyvalue,} are left alone:

In[5]:=
ResourceFunction["ToAssociations"][{a, {b -> 2}}]
Out[5]=

Possible Issues

ToAssociations stops at unevaluated expressions:

In[6]:=
ResourceFunction["ToAssociations"][{a -> 1, b :> {c -> {d -> 2}}}]
Out[6]=

Evaluate the held part to let ToAssociations continue:

In[7]:=
%[b]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information