Function Repository Resource:

LookupCases

Source Notebook

Look up all values of a key in a nested association

Contributed by: Gerli Jõgeva

ResourceFunction["LookupCases"][expr,key]

gives a list of all values associated with key in the expression expr.

ResourceFunction["LookupCases"][expr,key,levelspec]

gives a list of all values associated with key in the expression expr on levels specified by levelspec.

ResourceFunction["LookupCases"][expr,key,levelspec,n]

gives the first n values in expr associated with key.

ResourceFunction["LookupCases"][key]

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

Details and Options

LookupCases uses standard level specifications:
nlevels 1 through n
Infinitylevels 1 through Infinity
{n}level n only
{n,m}levels n through m
Alllevels 0 through Infinity
The default value for levelspec in ResourceFunction["LookupCases"] is {0,Infinity} or All.
The default value for the number of values n is Infinity.
ResourceFunction["LookupCases"] traverses the parts of expr in a depth-first order, with leaves visited before roots.

Examples

Basic Examples (2) 

LookupCases allows you to look up the values of a key anywhere in the structure, without knowing its location:

In[1]:=
ResourceFunction[
 "LookupCases"][<|"A1" -> <|"B" -> 1, "C" -> 2|>, "A2" -> <|"B" -> 3, "C" -> 4|>|>, "B"]
Out[1]=

Use the operator form:

In[2]:=
ResourceFunction["LookupCases"]["B"][<|"A1" -> <|"B" -> 1, "C" -> 2|>,
   "A2" -> <|"B" -> 3, "C" -> 4|>|>]
Out[2]=

Scope (6) 

An empty list is returned when no matches are found:

In[3]:=
ResourceFunction["LookupCases"][<|"A" -> 1|>, "B"]
Out[3]=

Looking up a delayed value causes the value to evaluate:

In[4]:=
ResourceFunction["LookupCases"][<|"A" :> (Print[1]; 2)|>, "A"]
Out[4]=

If the delayed value is in a non-matching key, evaluation does not occur:

In[5]:=
ResourceFunction[
 "LookupCases"][<|"A" -> 1, "B" :> (Print[1]; 2)|>, "A"]
Out[5]=

Do not look up values on the top-level association:

In[6]:=
ResourceFunction[
 "LookupCases"][<|"A" -> <|"A" -> 1|>|>, "A", {1, Infinity}]
Out[6]=

Only return the first match:

In[7]:=
ResourceFunction[
 "LookupCases"][<|"A1" -> <|"B" -> 1, "C" -> 2|>, "A2" -> <|"B" -> 3, "C" -> 4|>|>, "B", All, 1]
Out[7]=

LookupCases also finds the value from a list of rules:

In[8]:=
ResourceFunction["LookupCases"][{<|"A" -> 1|>, {"A" -> 2}}, "A"]
Out[8]=

If the list contains other items besides rules, the match is not found:

In[9]:=
ResourceFunction["LookupCases"][{"A" -> 1, "B"}, "A"]
Out[9]=
In[10]:=
ResourceFunction["LookupCases"][{"A" -> 1, "B" -> 2}, "A"]
Out[10]=

Use a pattern for the key:

In[11]:=
ResourceFunction[
 "LookupCases"][<|"A1" -> <|"B1" -> 1, "C1" -> 2|>, "A2" -> <|"B2" -> 3, "C2" -> 4|>|>, _String?(StringMatchQ["B*"])]
Out[11]=

Possible Issues (1) 

In a nested structure with the same key, both values are returned:

In[12]:=
ResourceFunction["LookupCases"][<|"A" -> <|"A" -> 1|>|>, "A"]
Out[12]=

Publisher

Gerli Jogeva

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 27 March 2019

Related Resources

License Information