Wolfram Computation Meets Knowledge

The Wolfram Language:
Fast Introduction for Programmers

Show additional notes for:
All sections 
Get Started

Associations Video Version

Notes for Java programmers:

Wolfram Language Associations are similar to Java's Map or HashMap, but with more readable syntax and much more flexible usage.

Notes for Python programmers:

Associations in the Wolfram Language work similarly to Python dictionaries. Using an Association, programmers can associate keys to values with highly efficient lookup and updating, even with millions of elements, and they also preserve the order of element insertion without needing specialized data structures like Python's OrderedDict. When coding in Python with the Wolfram Client Library for Python, classes like dict, OrderedDict and Series (from the Pandas library) serialize to Association.

Associations associate keys and values:
(→ is typed using ->)

<|"a" -> x, "b" -> y|>

(The keys in associations don't have to be strings, but often are.)

Applying an association to a key gives the corresponding value:


In a pure function, #key picks out the value corresponding to "key" in an association:

{#b, 1+#b} & [<|"a"->x, "b" -> y |>]

You can mix associations and lists, and pick out parts using [[ ... ]]:

<|"a" -> x, "b" -> {5, 6}|>[["b", 1]]

String templates work with associations (as do XML and notebook templates):

TemplateApply["first `a`; second `b`; first `a`", <|"a" -> x, "b" -> y|>]


In the association <|"names" {"john", "ann"}, "numbers" {2, 3}|>, which of the following does NOT give the "names" element?

Which of these extracts the age of the cat in the association
x=<|"cat" <|"vet" "Eve", "age" 5|>, "dog" <|"vet" "Abe", "age" 4|>|>?

Which of the following applies a template to make a string with appetizer and dessert from the association
meal=<|"appetizer" "nachos", "salad" "spinach", "dessert" "chocolate"|>?

© 2023 Wolfram. All rights reserved.