Function Repository Resource:

KeyIndex

Source Notebook

Create an association indexed by a given key from a list of associations

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["KeyIndex"][{a1,a2,},key]

gives an Association that has keys ai[key] and values ai with key dropped.

ResourceFunction["KeyIndex"][key]

represents an operator form of ResourceFunction["KeyIndex"] that can be applied to an expression.

Details and Options

Each of the ai can be either an Association or a List of rules.
In ResourceFunction["KeyIndex"][expr,key] for non-atomic expr, the expr can have any head, not just List.
ResourceFunction["KeyIndex"][Dataset[],] is also supported when the dataset is formed from a list of associations.
ResourceFunction["KeyIndex"][assoc,] is equivalent to ResourceFunction["KeyIndex"][Values[assoc],] when assoc is an Association.
ResourceFunction["KeyIndex"] has the following options:
"DropKey"Truewhether to drop key from each of the ai
MergingFunctionAutomaticdetermine how ai and aj should be combined when ai[key]aj[key]
MissingBehaviorAutomatichow to handle ai that do not have key
The values for MissingBehavior determine how to handle ai that do not have the given key:
Automaticdiscard ai that lack the given key
Noneinclude ai under the key Missing["KeyAbsent",key] in the result

Examples

Basic Examples (5) 

Index a list of associations by a given key:

In[1]:=
ResourceFunction["KeyIndex"][{<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>},
  a]
Out[1]=

Index lists of rules:

In[2]:=
ResourceFunction["KeyIndex"][{{a -> 1, b -> 2}, {a -> 3, b -> 4}}, a]
Out[2]=

Use a combination of lists and associations:

In[3]:=
ResourceFunction["KeyIndex"][{<|a -> 1, b -> 2|>, {a -> 3, b -> 4}},
  a]
Out[3]=

Use the operator form:

In[4]:=
ResourceFunction["KeyIndex"][a]
Out[4]=
In[5]:=
%[{<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>}]
Out[5]=

Index a Dataset by a given key:

In[6]:=
data = RandomSample[ResourceData["Meteorite Landings"], 5]
Out[6]=
In[7]:=
ResourceFunction["KeyIndex"][data, "ID"]
Out[7]=

Scope (2) 

The head of expr doesn’t necessarily have to be List:

In[8]:=
ResourceFunction["KeyIndex"][
 h[<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>], a]
Out[8]=

If expr is an Association, KeyIndex uses Values[expr]:

In[9]:=
ResourceFunction[
 "KeyIndex"][<|"a" -> <|a -> 1, b -> 2|>, "b" -> <|a -> 3, b -> 4|>|>,
  a]
Out[9]=

Options (7) 

DropKey (2) 

Drop the given key from each of the resulting associations:

In[10]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>}, a, "DropKey" -> True]
Out[10]=

Leave the given key in the results:

In[11]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>}, a, "DropKey" -> False]
Out[11]=

MergingFunction (3) 

By default, only the first value will be used when there is a collision:

In[12]:=
ResourceFunction["KeyIndex"][{<|a -> 1, b -> 2|>, <|a -> 1, b -> 3|>},
  a]
Out[12]=

Specify how collisions should be handled:

In[13]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, <|a -> 1, b -> 3|>}, a, MergingFunction -> Identity]
Out[13]=

Combine values with Merge:

In[14]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2, c -> 3|>, <|a -> 1, b -> 4, c -> 5|>, <|a -> 2, b -> 6, c -> 7|>}, a, MergingFunction -> Merge[Identity]]
Out[14]=

MissingBehavior (2) 

The default behavior of KeyIndex is to discard associations that lack the given key in the result:

In[15]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, <|b -> 3, c -> 4|>, <|a -> 5, c -> 6|>}, a, MissingBehavior -> Automatic]
Out[15]=

Don’t apply special rules if the key is missing:

In[16]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, <|b -> 3, c -> 4|>, <|a -> 5, c -> 6|>}, a, MissingBehavior -> None]
Out[16]=

Applications (2) 

Create a new Dataset from an existing one that is indexed by a particular key:

In[17]:=
data = ResourceData["Meteorite Landings"]
Out[17]=
In[18]:=
byID = ResourceFunction["KeyIndex"][data, "ID"]
Out[18]=

Look up data using the new index:

In[19]:=
byID[1337]
Out[19]=

Create groups by a given key:

In[20]:=
byClassification = ResourceFunction["KeyIndex"][ResourceData["Meteorite Landings"], "Classification", MergingFunction -> Identity]
Out[20]=

Get the set matching a given classification:

In[21]:=
byClassification["Iron"]
Out[21]=

Properties and Relations (2) 

KeyIndex[expr,key] is similar to GroupBy[expr,Key[key]]:

In[22]:=
data = {<|a -> 1, b -> 2|>, <|a -> 3, b -> 4|>, <|b -> 5, c -> 6|>}
Out[22]=
In[23]:=
ResourceFunction["KeyIndex"][data, a]
Out[23]=
In[24]:=
GroupBy[data, Key[a]]
Out[24]=

Similar behavior can be obtained by setting options:

In[25]:=
ResourceFunction["KeyIndex"][data, a, MergingFunction -> Identity, "DropKey" -> False, MissingBehavior -> None]
Out[25]=

The default behavior of KeyIndex does additional post-processing of the data:

In[26]:=
ResourceFunction["KeyIndex"][data, a]
Out[26]=
In[27]:=
KeyDrop[Map[First, GroupBy[data, Key[a] -> KeyDrop[a]]], Missing["KeyAbsent", a]]
Out[27]=

Possible Issues (1) 

Each item must be an Association or a List of rules:

In[28]:=
ResourceFunction[
 "KeyIndex"][{<|a -> 1, b -> 2|>, a -> 3, <|a -> 4, b -> 5|>}, a]
Out[28]=
In[29]:=
ResourceFunction["KeyIndex"][a -> 1, a]
Out[29]=
In[30]:=
ResourceFunction["KeyIndex"][x, a]
Out[30]=

Version History

  • 1.0.0 – 22 May 2019

Related Resources

License Information