Wolfram Research

LookupKeys

Contributed by: Taliesin Beynon

Source Notebook

Look up a set of keys applying a function when a key is missing

ResourceFunction["LookupKeys"][assoc,keys,func]

looks up a set of keys, calling func[key] if a key is missing from assoc.

Examples

Basic Examples

Ask for a key:

In[1]:=
ResourceFunction["LookupKeys"][
 Association[{a -> x, b -> y, c -> z}], {a}]
Out[1]=

If a key is missing:

In[2]:=
ResourceFunction["LookupKeys"][
 Association[{a -> x, b -> y, c -> z}], {b, d}, missing]
Out[2]=

Applications

Get some data when a key is missing:

In[3]:=
ResourceFunction["LookupKeys"][
 PlanetData[PlanetData[], "Radius", "EntityAssociation"], {Entity["Planet", "Earth"], Entity["MinorPlanet", "Pluto"]}, MinorPlanetData[#, "Radius"] &]
Out[3]=

Resource History