Function Repository Resource:

AssociationKeyDeflatten

Source Notebook

Deflatten the keys in a flat Association to create a nested Association

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["AssociationKeyDeflatten"][<|{k1,1,k1,2,}val1,{k2,1,k2,2,}val2,|>]

gives a nested Association assoc where each valn is found at assoc[kn,1,kn,2,].

ResourceFunction["AssociationKeyDeflatten"][assoc,f]

uses f to interpret the keys of assoc as lists.

Details

ResourceFunction["AssociationKeyDeflatten"] is an inverse for the resource function AssociationKeyFlatten.

Examples

Basic Examples (4) 

Create a nested association from a flat association with lists as keys:

In[1]:=
ResourceFunction[
 "AssociationKeyDeflatten"][<|{a, x} -> 1, {a, y} -> 2|>]
Out[1]=

Keys not wrapped in a list are unchanged:

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

Provide a function to interpret keys as lists:

In[3]:=
ResourceFunction[
 "AssociationKeyDeflatten"][<|"a x" -> 1, "a y" -> 2, "b x" -> 3|>, StringSplit]
Out[3]=

Values held by RuleDelayed will not be evaluated:

In[4]:=
ResourceFunction[
 "AssociationKeyDeflatten"][<|{a, x} :> 1 + 1, {a, y} -> 2 + 2|>]
Out[4]=

Scope (1) 

AssociationKeyDeflatten also works with lists of rules:

In[5]:=
ResourceFunction[
 "AssociationKeyDeflatten"][{{a, x} -> 1, {a, y} -> 2}]
Out[5]=
In[6]:=
ResourceFunction[
 "AssociationKeyDeflatten"][{"a x" -> 1, "a y" -> 2, "b x" -> 3}, StringSplit]
Out[6]=

Properties and Relations (2) 

Deflatten the keys of an association:

In[7]:=
assoc = <|{Subscript[k, 1], Subscript[k, 11]} -> a, {Subscript[k, 1], Subscript[k, 12]} -> b, {Subscript[k, 2], Subscript[k, 21], Subscript[k, 211]} -> c, {Subscript[k, 2], Subscript[k, 21], Subscript[k, 212]} -> d, {Subscript[k, 3]} -> {}, {Subscript[k, 4]} -> e|>
Out[7]=
In[8]:=
ResourceFunction["AssociationKeyDeflatten"][assoc]
Out[8]=

Use AssociationKeyFlatten to get the original association:

In[9]:=
ResourceFunction["AssociationKeyFlatten"][%]
Out[9]=

If f and g are partial inverses of each other, AssociationKeyDeflatten[,f] and AssociationKeyFlatten[,g] typically will be as well:

In[10]:=
deflatten = ResourceFunction["AssociationKeyDeflatten"][#, StringSplit] &;
flatten = ResourceFunction["AssociationKeyFlatten"][#, StringRiffle] &;
In[11]:=
assoc = <|"a x" -> 1, "a y" -> 2, "b x" -> 3|>
Out[11]=
In[12]:=
deflatten[assoc]
Out[12]=
In[13]:=
flatten[%]
Out[13]=
In[14]:=
% === assoc
Out[14]=

Neat Examples (3) 

Convert an association where keys are file paths into a nested association:

In[15]:=
directorySizes = KeyMap[
  ResourceFunction["RelativePath"][$InstallationDirectory, #] &,
  AssociationMap[
   ResourceFunction["BytesToQuantity"]@*ResourceFunction[
    "DirectorySize"],
   Select[FileNames["*", $InstallationDirectory, {2}], DirectoryQ]
   ]
  ]
Out[15]=
In[16]:=
nested = ResourceFunction["AssociationKeyDeflatten"][directorySizes, FileNameSplit]
Out[16]=

Get everything under a particular directory:

In[17]:=
nested["AddOns"]
Out[17]=

Revert to the flat association:

In[18]:=
ResourceFunction["AssociationKeyFlatten"][nested, FileNameJoin]
Out[18]=
In[19]:=
% === directorySizes
Out[19]=

Version History

  • 1.0.1 – 12 November 2024
  • 1.0.0 – 30 September 2020

Related Resources

License Information