Function Repository Resource:

# LookupCases

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:
 n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n,m} levels n through m All levels 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]:=
 Out[1]=

Use the operator form:

 In[2]:=
 Out[2]=

### Scope (6)

An empty list is returned when no matches are found:

 In[3]:=
 Out[3]=

Looking up a delayed value causes the value to evaluate:

 In[4]:=
 Out[4]=

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

 In[5]:=
 Out[5]=

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

 In[6]:=
 Out[6]=

Only return the first match:

 In[7]:=
 Out[7]=

LookupCases also finds the value from a list of rules:

 In[8]:=
 Out[8]=

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

 In[9]:=
 Out[9]=
 In[10]:=
 Out[10]=

Use a pattern for the key:

 In[11]:=
 Out[11]=

### Possible Issues (1)

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

 In[12]:=
 Out[12]=

Gerli Jogeva

## Requirements

Wolfram Language 11.3 (March 2018) or above

## Version History

• 1.0.0 – 27 March 2019