Function Repository Resource:

NestedLookup

Source Notebook

Look up a set of keys in order to get deeper parts of an association or list of rules

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["NestedLookup"][assoc,{key1,key2,}]

looks up the value associated with the keys key1,key2, in deeper levels of assoc.

ResourceFunction["NestedLookup"][assoc,keys, default]

gives default if the part specified by keys is not found.

ResourceFunction["NestedLookup"][{key1,key2,}]

represents an operator form of ResourceFunction["NestedLookup"] that can be applied to an expression.

Details and Options

ResourceFunction["NestedLookup"][assoc,{key1,key2,}] is effectively equivalent to Fold[Lookup,assoc,{key1,key2,}] but returns Missing["KeySequenceAbsent",keys] if the specified part is not found.
In ResourceFunction["NestedLookup"][assoc,keys,default], default is only evaluated if the key sequence keys has any part missing.
ResourceFunction["NestedLookup"] can be applied to expressions that are Association objects or lists of rules at each level.
ResourceFunction["NestedLookup"][assoc,{,Key[list],}] treats the list list as a single key rather than a list of keys.
ResourceFunction["NestedLookup"][{key1,key2,}][assoc] is equivalent to ResourceFunction["NestedLookup"][assoc,{key1,key2,}].

Examples

Basic Examples (4) 

Retrieve the ResourceFunction:

In[1]:=
ResourceFunction["NestedLookup"]
Out[1]=

Look up a nested part of an association:

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

When a key is not found, a Missing object is returned:

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

When a third argument is provided and the key sequence has a part missing, the default value is returned:

In[4]:=
ResourceFunction["NestedLookup"][<|"a" -> <|"b" -> 1|>|>, {"a", "c"},
  0]
Out[4]=

Scope (5) 

When the key sequence is present, the default is not evaluated:

In[5]:=
ResourceFunction["NestedLookup"][<|"a" -> <|"b" -> 1|>|>, {"a", "b"}, Print["not found"]]
Out[5]=

NestedLookup threads over lists of associations:

In[6]:=
table = {
   <|"a" -> <|"b" -> 1|>|>,
   <|"a" -> <|"b" -> 2|>|>,
   <|"a" -> <|"b" -> 3|>|>
   };
In[7]:=
ResourceFunction["NestedLookup"][table, {"a", "b"}]
Out[7]=

NestedLookup can be used to look up keys in lists of rules:

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

Lists of rules can be mixed with Associations:

In[9]:=
ResourceFunction["NestedLookup"][{"a" -> <|"b" -> 1|>}, {"a", "b"}]
Out[9]=

Use the operator form of NestedLookup:

In[10]:=
ResourceFunction["NestedLookup"][{"a", "b"}]@<|"a" -> <|"b" -> 1|>|>
Out[10]=

Applications (1) 

You can use constructs such as Throw and Return in the third argument of NestedLookup to abort a computation when a required key is missing:

In[11]:=
calculateBMI[stats_] := Module[{w, h},
  {w, h} = ResourceFunction["NestedLookup"][
    stats, {"Info", {"Weight", "Height"}}, Return[$Failed, Module]];
  w/h^2
  ]
In[12]:=
calculateBMI[<|"Info" -> <|"Weight" -> 90, "Height" -> 1.8|>|>]
Out[12]=
In[13]:=
calculateBMI[<|"Info" -> <|"Weight" -> 90|>|>]
Out[13]=

Properties and Relations (3) 

Applying an Association to a sequence of keys is typically equivalent to using NestedLookup on a list of those keys:

In[14]:=
ResourceFunction["NestedLookup"][<|"a" -> <|"b" -> 1|>|>, {"a", "b"}]
Out[14]=
In[15]:=
<|"a" -> <|"b" -> 1|>|>["a", "b"]
Out[15]=

Lists are handled differently by NestedLookup, however:

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

Wrap the list in Key to make NestedLookup treat it as a single key:

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

Possible Issues (2) 

When the key to be looked up is a list or an expression with head Key, it must be wrapped in Key to avoid ambiguity:

In[19]:=
assoc = <|Key[a] -> <|d -> 1|>, {b, c} -> 2|>
Out[19]=
In[20]:=
ResourceFunction["NestedLookup"][assoc, {Key[a], d}]
Out[20]=
In[21]:=
ResourceFunction["NestedLookup"][assoc, {Key[Key[a]], d}]
Out[21]=
In[22]:=
ResourceFunction["NestedLookup"][assoc, {{b, c}}]
Out[22]=
In[23]:=
ResourceFunction["NestedLookup"][assoc, {Key[{b, c}]}]
Out[23]=

When a part is not found, NestedLookup only reports the sequence up to the missing key:

In[24]:=
ResourceFunction[
 "NestedLookup"][<|"a" -> <|"b" -> <|"c" -> 1|>|>|>, {"a", "x", "c"}]
Out[24]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 12 October 2018

Related Resources

License Information