Wolfram: Association (Key Value List)

By Xah Lee. Date: . Last updated: .

What is Association

Wolfram language's Association is a dedicated structure for list of key value pairs. It provides a efficient implementation of ordered list of pairs, when you have large number of pairs such as over a thousand pairs.

it's also known as hashtable, map, dictionary, or association list.

Association Syntax

Association[rule1, rule2, etc]

🔸 SHORT SYNTAX: <|args|>

Association is an ordered list of key-value pairs.

Each rule is one of:

  • Rule[key, val]
  • RuleDelayed[key, val]

〔see Wolfram: Rule, RuleDelayed

  • The key can be any Expression.
  • The value can be any Expression.
  • If a key already exist, the earlier one is removed.
(* create a association. string keys *)
xx = Association[ "a" -> 3, "b" -> 2 ]
(* <|a -> 3, b -> 2|> *)

(* short syntax *)
yy = <| "a" -> 3, "b" -> 2 |>
(* <|a -> 3, b -> 2|> *)

xx === yy
(* True *)

Symbol keys

(* create a association, symbol keys *)
xx = Association[ a -> 3, b -> 2 ]
(* <|a -> 3, b -> 2|> *)

Integer keys

(* create a association, integer keys *)
xx = Association[ 5 -> 3, 4 -> 2 ]
(* <|5 -> 3, 4 -> 2|> *)

Arbitrary Expression as Keys

(* create a association, arbitrary expression as keys *)
xx = Association[ f[x] -> 3, Sin[b] -> 2 ]
(* <|f[x] -> 3, Sin[b] -> 2|> *)
Association[{args}]

same as Association[args]

Length

Length[ asso ]

number of items.

Check If is Association

AssociationQ[expr]

return true if expr is an association.

xx = Association[ "a" -> 3, "b" -> 2 ];
AssociationQ[xx]
(* True *)

Wolfram. Association (Key Value List)

Key value pairs in different programing languages