Lookup

Lookup[assoc,key]
looks up the value associated with key in the association assoc; if the key is not present, Missing["KeyAbsent",key] is returned.

Lookup[assoc,{key1,key2,}]
gives a list of the values associated with the .

Lookup[{assoc1,assoc2,},key]
gives a list corresponding to the value of key in each .

Lookup[assoc,key,default]
gives default if the key is not present.

Lookup[key]
represents an operator form of Lookup that can be applied to an expression.

DetailsDetails

  • In Lookup[assoc,key,default], default is only evaluated if key is not found in assoc.
  • Lookup can be applied not only to Association objects, but also to lists of rules.
  • Lookup[assoc,Key[list]] treats the list list as a single key rather than a list of keys.
  • Lookup[{assoc1,assoc2,},{key1,key2,}] gives an array of the form .
  • Lookup[key][assoc] is equivalent to Lookup[assoc,key].

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

Look up the value associated with the key:

In[1]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input
Out[2]=

Providing a third argument allows the default value to be provided:

In[1]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input
Out[2]=

Lookup threads over lists of associations:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=

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

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Use the operator form of Lookup:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 2014
(10.0)