Associations are a kind of generalization of lists, in which every element has a key as well as a value. Counts is a typical function that produces an association.

Counts[{a,a,b,c,a,a,b,c,c,a,a}]

You can get the value associated with a particular key using [...].

\[LeftAssociation]a->6,b->2,c->3\[RightAssociation][c]

Operations that work on lists typically also work on associations—but apply themselves only to the values, not the keys.

\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]+500

f/@\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]

Total[\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]]

Sort[\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]]

KeySort[\[LeftAssociation]c->1,b->2,a->4\[RightAssociation]]

The functions Keys and Values extract the keys and values of an association.

Keys[\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]]

Values[\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]]

Normal turns an association into a normal list of rules. Association makes an association from a list of rules.

Normal[\[LeftAssociation]a->6,b->2,c->3\[RightAssociation]]

Association[{a->6,b->2,c->3}]

LetterCounts counts how many times letters occur in a string.

LetterCounts[WikipediaData["computers"]]

KeyTake picks out elements of an association that appear in a list of keys you specify. Here we’re taking the elements whose keys are letters in the (lowercase) alphabet.

KeyTake[LetterCounts[WikipediaData["computers"]],Alphabet[ ]]

BarChart plots the values in an association. With the option ChartLabels->Automatic, it uses the keys as labels.

BarChart[KeyTake[LetterCounts[WikipediaData["computers"]],Alphabet[ ]],ChartLabels->Automatic]

Here’s a direct way to apply a pure function to an association:

f[#["apples"],#["oranges"]]&[\[LeftAssociation]"apples"->10,"oranges"->12,"pears"->4\[RightAssociation]]

It’s very common to have keys that are strings, and the Wolfram Language has a special way to handle these when it comes to pure functions: you can just use #key to refer to an element whose key is "key".

f[#apples,#oranges]&[\[LeftAssociation]"apples"->10,"oranges"->12,"pears"->4\[RightAssociation]]

As a more realistic example, apply a pure function that extracts the value for “e” from the letter counts, and divides by the total. The N gives the result as a decimal.

#e/Total[#]& @LetterCounts[WikipediaData["computers"]]//N

Make a list, in order, of the number of times each of the digits 0 through 9 occurs in 3^100. »

Make a labeled bar chart of the number of times each of the digits 0 through 9 occurs in 2^1000. »

Make a labeled bar chart of the number of times each possible first letter occurs in words from WordList[ ]. »

Make an association giving the 5 most common first letters of words in WordList[ ] and their counts. »

Find the numerical ratio of the number of occurrences of “q” and “u” in the Wikipedia entry for computers. »

Find the 10 most common words in ExampleData[{"Text","AliceInWonderland"}]. »

Why are associations called “associations”?

Because they associate values with keys. Other names used for the same concept are associative arrays, dictionaries, hashmaps, structs, key-value maps and symbolically indexed lists.

How does one type in an association?

Start with <| (< followed by |), then use -> for each ->.  Alternatively use Association[a->1,b->2].

Can an association have several elements with the same key?

No. Keys in an association are always unique.

What happens if I ask for a key that’s not in an association?

Normally you get Missing[...]. But if you use Lookup to look up the key, you can specify what to return if the key is absent.

How can I do operations on the keys of an association?

Use KeyMap, or use functions like KeySelect and KeyDrop. AssociationMap creates an association by mapping a function over keys.

How can I combine several associations into one?

Use Merge. You have to give a function to say what to do if the same key occurs in multiple associations.

Can one use [[...]] to extract part of an association, like one extracts part of a list?

Yes, if you explicitly say assoc[[Key[key]]]. For example,  assoc[[2]] will extract the second element of assoc, whatever key it has. assoc[[key]] is a special case that works the same as assoc[key].

What happens in pure functions if the keys in an association aren’t strings?

You can’t use #key anymore; you have to explicitly use #[key].

Tech Notes