Lookup

Lookup[assoc,key]
looks up the value associated with key in the association assoc, or Missing["KeyAbsent"].

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

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

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

DetailsDetails

  • In Lookup[assoc,key,def], def 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[{assoc1,assoc2,},{key1,key2,}] gives an array of the form .

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]=

Look up the value using bracket notation:

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

When the key is not found, the default value is returned:

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

When looking up using bracket notation, a Missing is returned:

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

The default third argument may be omitted:

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

The default is only evaluated if it is needed:

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

Lookup can be used for lists:

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