Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Deflatten the keys in a flat Association to create a nested Association
| 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. | 
Create a nested association from a flat association with lists as keys:
| In[1]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][<|{a, x} -> 1, {a, y} -> 2|>]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/16653c0cbefd908c.png) | 
| Out[1]= |  | 
Keys not wrapped in a list are unchanged:
| In[2]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][<|{a, x} -> 1, {a, y} -> 2, b -> 3|>]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/5e60323fd6b2f7d8.png) | 
| Out[2]= |  | 
Provide a function to interpret keys as lists:
| In[3]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][<|"a x" -> 1, "a y" -> 2, "b x" -> 3|>, StringSplit]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/252d96c2080601f1.png) | 
| Out[3]= |  | 
Values held by RuleDelayed will not be evaluated:
| In[4]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][<|{a, x} :> 1 + 1, {a, y} -> 2 + 2|>]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/235858400e169b0a.png) | 
| Out[4]= |  | 
AssociationKeyDeflatten also works with lists of rules:
| In[5]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][{{a, x} -> 1, {a, y} -> 2}]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/578c55debc994fa1.png) | 
| Out[5]= |  | 
| In[6]:= | ![ResourceFunction[
 "AssociationKeyDeflatten"][{"a x" -> 1, "a y" -> 2, "b x" -> 3}, StringSplit]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/6befbc57ea81ec05.png) | 
| Out[6]= |  | 
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|>](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/4ff20d0fbdb8e82f.png) | 
| Out[7]= |  | 
| In[8]:= | ![ResourceFunction["AssociationKeyDeflatten"][assoc]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/4802242a8412e43b.png) | 
| Out[8]= |  | 
Use AssociationKeyFlatten to get the original association:
| In[9]:= | ![ResourceFunction["AssociationKeyFlatten"][%]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/34b18d0e1f28c08c.png) | 
| 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] &;](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/63743210209067cc.png) | 
| In[11]:= |  | 
| Out[11]= |  | 
| In[12]:= | ![deflatten[assoc]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/2a6df7b303a4a2c4.png) | 
| Out[12]= |  | 
| In[13]:= | ![flatten[%]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/0a8d1085e7505512.png) | 
| Out[13]= |  | 
| In[14]:= |  | 
| Out[14]= |  | 
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]
   ]
  ]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/07368bfa06f9b6aa.png) | 
| Out[15]= |  | 
| In[16]:= | ![nested = ResourceFunction["AssociationKeyDeflatten"][directorySizes, FileNameSplit]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/684ff6be6e389ff7.png) | 
| Out[16]= |  | 
Get everything under a particular directory:
| In[17]:= | ![nested["AddOns"]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/3e78aa0356ac78d6.png) | 
| Out[17]= |  | 
Revert to the flat association:
| In[18]:= | ![ResourceFunction["AssociationKeyFlatten"][nested, FileNameJoin]](https://www.wolframcloud.com/obj/resourcesystem/images/e86/e8662614-c2e7-483a-ad65-d44c35c9224e/3871ca4c8ee3d1b0.png) | 
| Out[18]= |  | 
| In[19]:= |  | 
| Out[19]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License