WolframLang: Association (Hashtable, Map, Dictionary)

By Xah Lee. Date: . Last updated: .

WolframLang's Association is a dedicated structure for list of key/value pairs. Similar to Lisp Association List, JavaScript Map, Python Dictionary, Perl Hash Table. It provides a efficient implementation of ordered list of pairs, when you have large number of pairs such as over a hundred pairs.

Create Association

Association[rule1, rule2 etc]
(short syntax: <|args|> is same as Association[args] )
Each rule is one of:
  • Rule[expr1, expr2] (short syntax expr1 -> expr2)
  • RuleDelayed[expr1, expr2] (short syntax expr1 :> expr2)

An ordered list of key/value pairs.
  • The key or value can be any expression.
  • The key is usually a Symbol or a string.
  • If key are duplicated, the earlier ones are removed.
Association
(* create a key/value pairs list *)
xa = Association[ a -> 3, b -> 2 ]
Association[{args}]
same as Association[args]

Count/Length

Length[ assoc ]
return number of pairs.
Length

Add a Key/Val Pair

Note: by WolframLang convention, function whose name ends in “To” or “From” modifies a variable.

assocVar[key]=val
Add a key value pair to assocVar. If the key exist, value is updated. assocVar must be a variable.
AssociateTo[ assocVar, rule ]
  • Add a entry to the variable and modify the variable. The first arg must be a variable. Its value must be a Association. Return a new Association.
  • If the key exist, value is updated.
AssociateTo
xa = Association[ a -> 3 ];
AssociateTo[ xa, {c -> 4} ];

xa
(*
<|a -> 3, c -> 4|>
*)
AssociateTo[ assocVar, {rule1, rule2 etc} ]
Add multiple entries.
xa = Association[ a -> 3 ];
AssociateTo[ xa, {c -> 4, m -> 9} ];

xa
(*
<|a -> 3, c -> 4, m -> 9|>
 *)
AppendTo
Append one or more entries. Use list for more than one.
If the key exist, value is updated.

AppendTo
xa = Association[ a -> 3, b -> 2 ]
AppendTo[ xa, {b -> 4, m -> 1} ]
(*
<|a -> 3, b -> 4, m -> 1|>
 *)

Delete a Key/Val Pair

KeyDropFrom[assocVar, key]
remove the element with the specified key. KeyDropFrom
KeyDropFrom[assocVar,{key1, key2, }]
remove many elements
xa = Association[ a -> 1, b -> 2, c -> 3, d -> 4, e -> 5 ]

KeyDropFrom[ xa, b ]
(*
<|a -> 1, c -> 3, d -> 4, e -> 5|>
*)

KeyDropFrom[ xa, {c, d} ]
(*
<|a -> 1, e -> 5|>
*)
KeyDrop
return a new association with some keys removed. (does not modify variable)
KeyDrop

Clear All Entries

Clear[assocVar]
clear the variable assocVar.

Check Key Exist

KeyExistsQ[ assoc, key ]
returns True if the key exist, else False.
KeyExistsQ
xa = Association[ a -> 3,  b -> 2 ];

KeyExistsQ[ xa, b ]
(* True *)

KeyExistsQ[ Association[ a -> 3,  b -> 2 ], b ]
(* True *)

Get Key's Value

Lookup[assoc, key]
looks up the value associated with key in the association assoc; if the key is not present, Missing["KeyAbsent", key] is returned. Lookup
Lookup[assoc,{key1, key2, }]
gives a list of the values associated with the keys.
Lookup[{assoc1, assoc2, }, key]
gives a list corresponding to the value of key in each associ.
Lookup[assoc, key, default]
gives default if the key is not present.
xa = Association[ a -> 3,  b -> 2 ];

Lookup[xa, b]
 (* 2 *)

Lookup[xa, c]
 (* Missing[KeyAbsent, c] *)

Lookup[xa, c, 99]
 (* 99 *)
assoc[key]
return the value of a key. return Missing["KeyAbsent", key] if key not exist. Missing

KeySelect — select elements based on a criterion on their keys

Get All Keys

Keys

Get All Values

Values


KeyTake[assoc,{key1, key2, }]
return an association containing only the elements with give keys. KeyTake
KeyTake[{assoc1, assoc2, }, keys]
gives a list of associations.

Elements of Associations

Functions That Apply to Values » Map ▪ Select ▪ Sort ▪ DeleteDuplicates ▪ ListPlot ▪ Plus ▪ ...

Functions That Apply to Keys

Pattern Matching with Associations

Functions That Create Associations

GroupBy
List To association list GroupBy

Functions Operating on Lists of Associations

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc