Association

Association[key1val1,key2val2,] or key1val1,key2val2,

represents an association between keys and values.

Details

  • The value associated with a given key can be extracted using assoc[key].
  • An Association acts like a symbolically indexed list. The value associated with a given key can be extracted by using the part specification Key[key]. If key is a string, Key can be omitted.
  • If key is not present in assoc, assoc[key] yields Missing["KeyAbsent",key].
  • assoc[key]=val, where assoc is a symbol whose value is an Association, 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[{key1->val1,}] gives <|key1->val1,|>.
  • 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.
  • Association can be input using the characters \[LeftAssociation] and \[RightAssociation].
  • Normal converts an Association to a list of rules.
  • KeyValuePattern can be used to represent a pattern for associations that include particular elements.

Examples

open allclose all

Basic Examples  (8)

An association in which key a is associated with value x, key b with value y, etc.:

Extract the value associated with key b:

Convert a list of rules to an association:

Convert an association to a list of rules:

Keys are "transparent" for many operations:

Position gives keys in an association:

Append to an association:

Delayed rules can be used to construct associations whose values are stored unevaluated:

The value in the association is evaluated when it is extracted:

Values in associations can be extracted like parts:

If the key in an association is a string, no explicit Key is needed to extract it as a part:

Numerical part specifications work structurally on associations:

Lookups in associations interoperate with parts in lists and other expressions:

The explicit Key can be omitted when the keys are strings:

Keys extracts the list of keys:

Associations can be modified by resetting values:

Scope  (11)

Associations work with any number of elements:

Extracting values from an association is highly efficient:

Associations can have arbitrary expressions as keys, including associations:

Missing elements are represented by Missing:

Lookup allows a default value to be given:

Associations can be used for pattern matching:

Extract key and value from an association:

Replace arguments of a function:

Pick the rule with the specific key:

Filter associations with length 2 only:

Define a function using an Association as an argument:

Use nested associations as a function argument:

KeyValuePattern lets you match any element in an association:

KeyValuePattern matches elements that appear anywhere in an association:

Properties & Relations  (5)

Values extracts values from an association:

Keys extracts keys from an association:

Normal turns an association into a list of rules:

Associations keep only the last instance of repeated keys:

When an association is modified, a new copy is created:

v is modified; w is unchanged:

Associations can have zero length:

Possible Issues  (2)

Association counts as one level, not two, in structural functions:

Lists of rules are two levels:

Replacing the head of an Association loses keys:

Extract rules from an Association:

Wolfram Research (2014), Association, Wolfram Language function, https://reference.wolfram.com/language/ref/Association.html (updated 2016).

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_2024_association, author="Wolfram Research", title="{Association}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Association.html}", note=[Accessed: 26-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_association, organization={Wolfram Research}, title={Association}, year={2016}, url={https://reference.wolfram.com/language/ref/Association.html}, note=[Accessed: 26-December-2024 ]}