Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Look up all values of a key in a nested association
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. |
| 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 |
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]= |
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]= |
In a nested structure with the same key, both values are returned:
| In[12]:= |
| Out[12]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License