Association[key1val1,key2val2,…] or key1val1,key2val2,…
represents an association between keys and values.
Association 
Association[key1val1,key2val2,…] or key1val1,key2val2,…
represents an association between keys and values.
Details
- The value associated with a given key k in the association assoc can be extracted using assoc[k]. »
- An association acts like a symbolically indexed list. The value associated with a given key k can be extracted using the part specification Key[k]. If k is a string, Key can be omitted. »
- If key is not present in assoc, assoc[key] yields Missing["KeyAbsent",key]. »
- If assoc is a symbol whose value is an association, assoc[key]=val can be used to associate or reset the association of key with val. »
- Typical list operations (such as Map, Select and Sort) apply to the values in an association, leaving the keys unchanged. »
- Association[{key1val1,…}] gives <|key1val1,…|>. »
- If there are multiple elements with the same key, all but the last of these elements are dropped. Merge yields instead a list of values for repeated keys.
- Associations can be input using the characters \[LeftAssociation] and \[RightAssociation]. These can be entered with
<|
and
|>
, respectively. - Normal converts an Association object to a list of rules. »
- KeyValuePattern can be used to represent a pattern for associations that include particular elements. »
Examples
open all close allBasic Examples (4)
An association in which key a is associated with value x, key b with value y, etc.:
<|a -> x, b -> y, c -> z|>Extract the value associated with key b:
%[b]Convert a list of rules to an association:
Association[{a -> x, b -> y, c -> z}]Associations can be modified by resetting values:
assoc = <|a -> x, b -> y, c -> z|>assoc[b] = wThe key b now has value w rather than y:
assocUsing delayed rules prevents the values from being evaluated during association construction:
<|a -> 1 + 1, b -> 2 + 2, c :> 3 + 3, d :> 4 + 4|>The values are evaluated when extracted:
%[c]Scope (17)
Construction and Extraction (9)
Associations can have arbitrary expressions as keys, including associations:
Keys[<|{2, 3} -> x, RGBColor[0.95, 0.43, 0.96] -> y, <|a -> 1, b -> 2|> -> z|>]Append[<|a -> x, b -> y, c -> z|>, d -> w]Associations work with any number of elements:
assoc = Association[Table[i -> i ^ 2, {i, 10000}]];Extracting values from an association is highly efficient:
assoc[10000]//EchoTimingMissing elements are represented by Missing:
<|a -> x, b -> y, c -> z|>[d]Lookup allows a default value to be given:
Lookup[<|a -> x, b -> y, c -> z|>, d, q]Extract a value from an association using Part:
assoc = <|a -> x, b -> {y, z}|>;
assoc[[Key[b]]]Extract a portion of the value:
assoc[[Key[b], 1]]The Key[k] part specification works at any level whose value is an association:
{5, 7, assoc}[[3, Key[b], 2]]Extract the third value in an association:
assoc = <|3 -> a, 1 -> b, 2 -> c|>;
assoc[[3]]Extract the value associated with the key 3:
assoc[[Key[3]]]Obtain the sub-association consisting of the second and third parts of an association:
assoc = <|3 -> a, 1 -> b, 2 -> c|>;
assoc[[{2, 3}]]assoc[[{Key[2], Key[3]}]]Extract the value associated with the key {2,3}:
assoc[[Key[{2, 3}]]]Extract a value using Extract:
assoc = <|a -> 1 + 1, b -> 2 + 2, c :> 3 + 3, d :> 4 + 4|>;
Extract[assoc, Key[c]]Obtain the value without letting it evaluate:
assoc = <|a -> 1 + 1, b -> 2 + 2, c :> 3 + 3, d :> 4 + 4|>;
Extract[assoc, Key[c], Hold]Separate an association into a list of keys and a list of values:
Comap[{Keys, Values}, <|a -> 1, b -> 2, c -> 3|>]Convert it to a list of ordered pairs:
Transpose[%]Pattern-Matching Operations (8)
Associations can be used for pattern matching:
MatchQ[<|a -> 1|>, <|_ -> _|>]Replace[<|a -> 1|>, <|a -> x_|> -> x]Extract key and value from an association:
<|1 -> 2|> /. <|a_ -> b_|> :> {a, b}Replace arguments of a function:
f[<|1 -> 2, 3 -> 4|>] /. f[<|a_ -> b_, ___|>] :> f[a, b]Pick the rule with the specific key:
<|1 -> 2, 2 -> 3, 3 -> 4, 5 -> 6|> /. <|___, 3 -> b_, ___|> :> f[b]Filter associations with length 2 only:
Cases[{<|1 -> x|>, <|1 -> x, 2 -> y|>, <|1 -> x, 2 -> y, 3 -> z|>}, <|_, _|>]Define a function using an association as an argument:
g[<|k_ -> v_|>] := vg[<|1 -> 2|>]Use nested associations as a function argument:
h[<|k_ -> <|k2_ -> v2_|>|>] := {k, k2, v2}h[<|1 -> <|2 -> 3|>|>]KeyValuePattern lets you match any element in an association:
MatchQ[<|a -> 1, b -> 2, c -> 3|>, KeyValuePattern[b -> 2]]KeyValuePattern matches elements that appear anywhere in an association:
Replace[<|a -> 1, b -> 2, c -> 3|>, KeyValuePattern[{a -> x_, c -> y_}] :> {x, y}]Properties & Relations (9)
Keys are "transparent" for typical list operations:
Map[f, <|a -> 4, b -> 2, c -> 1, d -> 5|>]Sort[<|a -> 4, b -> 2, c -> 1, d -> 5|>]Select[<|a -> 4, b -> 2, c -> 1, d -> 5|>, # > 3&]Total[<|a -> 4, b -> 2, c -> 1, d -> 5|>]Values extracts values from an association:
Values[<|a -> x, b -> y, c -> z|>]Keys extracts keys from an association:
Keys[<|a -> x, b -> y, c -> z|>]Normal turns an association into a list of rules:
Normal[<|a -> x, b -> y, c -> z|>]Position gives keys in an association:
Position[<|a -> 4, b -> 2, c -> 1, d -> 5|>, 2]Values in associations can be extracted like parts:
<|a -> x, b -> y, c -> z|>[[Key[b]]]If the key in an association is a string, no explicit Key is needed to extract it as a part:
<|"a" -> x, "b" -> y, "c" -> z|>[["b"]]Numerical part specifications work structurally on associations:
<|a -> x, b -> y, c -> z|>[[2]]Associations keep only the last instance of repeated keys:
<|a -> x, a -> xp, b -> y, c -> z, c -> zp|>Join[<|a -> x, b -> y, c -> z|>, <|a -> xp, c -> zp|>]When an association is modified, a new copy is created:
v = w = <|a -> 1, b -> 2|>v[a] = 3v is modified; w is unchanged:
{v, w}Associations can have zero length:
<||>Length[%]Possible Issues (2)
Association counts as one level, not two, in structural functions:
Level[<|a -> x, b -> y|>, {1}]Depth[<|a -> x, b -> y|>]Lists of rules are two levels:
Level[{a -> x, b -> y}, {1}]Depth[{a -> x, b -> y}]Replacing the head of an association loses keys:
ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> List]Extract rules from an association:
Normal[<|"x" -> 1, "y" -> 2|>]See Also
List Tabular Dataset Normal AssociationQ Lookup KeyExistsQ Keys Values KeyValuePattern SparseArray CompoundElement DataStructure Tree
Function Repository: AssociatePairs ToAssociations SparseAssociation AssociationEditor
Related Guides
-
▪
- Associations ▪
- Computation with Structured Datasets ▪
- Automated Reports ▪
- WDF (Wolfram Data Framework) ▪
- Language Overview ▪
- Setting Up Input Interpreters ▪
- Wolfram Language Syntax Characters ▪
- Expressions ▪
- Wolfram Language Syntax ▪
- Database Connectivity ▪
- Scientific Data Analysis ▪
- Wolfram Data Repository
Related Workflows
Text
Wolfram Research (2014), Association, Wolfram Language function, https://reference.wolfram.com/language/ref/Association.html (updated 2016).
CMS
Wolfram Language. 2014. "Association." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/Association.html.
APA
Wolfram Language. (2014). Association. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Association.html
BibTeX
@misc{reference.wolfram_2026_association, author="Wolfram Research", title="{Association}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Association.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_association, organization={Wolfram Research}, title={Association}, year={2016}, url={https://reference.wolfram.com/language/ref/Association.html}, note=[Accessed: 12-June-2026]}