### Basic Examples

Basic Examples

In[]:=

Because neither a “b” nor “e” is present in the first argument, the output association has values of 0 for those keys; otherwise the values are just as they would be with Counts:

[{"d","d","c","d","d","a","c","c","d"},CharacterRange["a","e"]]

In[]:=

a1,b0,c3,d5,e0

Out[]=

Keys not found are assumed to take on the default value of 42:

[{"d","d","c","d","d","a","c","c","d"},CharacterRange["a","e"],42]

In[]:=

a1,b42,c3,d5,e42

Out[]=

The association in the second argument results in missing keys being set to zero, except for key "b" which is set to 42:

[{"d","d","c","d","d","a","c","c","d"},\[LeftAssociation]"a"0,"b"42,"c"0,"d"0,"e"0\[RightAssociation]]

In[]:=

a1,b42,c3,d5,e0

Out[]=

An operator form of CountsList:

[CharacterRange["a","g"]][{"d","d","c","d","d","a","c","c","d"}]

In[]:=

a1,b0,c3,d5,e0,f0,g0

Out[]=

### Scope

Scope

In[]:=

Keys in the output have the same order as keys in the second argument of the input :

[{"d","d","c","d","d","a","c","c","d"},\[LeftAssociation]"e"0,"a"0,"b"42\[RightAssociation]]

In[]:=

e0,a1,b42

Out[]=

Users can specify their own defaults for a list of values:

[{"d","d","c","d","d","a","c","c","d"},{"a","d","b","x"},{3,1,4,0}]

In[]:=

a1,d5,b4,x0

Out[]=

But if one uses a list default, it must be the same length as the list of the keys: otherwise an error message is generated:

[{"d","d","c","d","d","a","c","c","d"},{"a","b"},{3,1,4,0}]

In[]:=

[{d,d,c,d,d,a,c,c,d},AssociationThread[{a,b},{3,1,4,0}]]

Out[]=

### Applications

Applications

In[]:=

A very common use of this function is producing association with the same length and same keys when using counting items in a structure of lists:

Map

[{"a","b"}],{{"a","b","b","a"},{"b","b"},{"a"}}

In[]:=

{a2,b2,a0,b2,a1,b0}

Out[]=

### Properties and Relations

Properties and Relations

In[]:=

Another way of going about this is to join a default with the , but this method is less flexible:

Join[\[LeftAssociation]"a"0,"b"42,"c"0,"d"0,"e"0\[RightAssociation],Counts[{"d","d","c","d","d","a","c","c","d"}]]

In[]:=

a1,b42,c3,d5,e0

Out[]=

One might also do it with a and use the argument for merging, but this method is similarly less flexible:

Merge[{Counts[{"d","d","c","d","d","a","c","c","d"}],\[LeftAssociation]"a"0,"b"42,"c"0,"d"0,"e"0\[RightAssociation]},First]

In[]:=

d5,c3,a1,b42,e0

Out[]=

### Possible Issues

Possible Issues

In[]:=

Duplicate keys in the second argument are eliminated:

[{"d","d","c","d","d","a","c","c","d"},{"a","c","a"}]

In[]:=

a1,c3

Out[]=

An empty list in the second argument or an empty Association produces an empty :

[{"d","d","c","d","d","a","c","c","d"},{}],

[{"d","d","c","d","d","a","c","c","d"},Association[]]

In[]:=

{,}

Out[]=

The second argument must be a or :

[{"d","d","c","d","d","a","c","c","d"},a[2,3]]

In[]:=

[{d,d,c,d,d,a,c,c,d},a[2,3]]

Out[]=

\[LeftAssociation]\[RightAssociation],

[{"d","d","c","d","d","a","c","c","d"},a[2,3]]

In[]:=

,

[{d,d,c,d,d,a,c,c,d},a[2,3]]

Out[]=

### Neat Examples

Neat Examples

In[]:=

Group the passengers on the Titanic over age 70 by age and count their genders:

QuerySelect[#age>70&]/*GroupBy[#age&]/*KeySort,

[{"male","female"}],#sex&[ExampleData[{"Dataset","Titanic"}]]

In[]:=

Out[]=

Find the distribution of UFO shapes by states, but limit oneself to the 5 most frequently seen shapes :