KeyValuePattern

KeyValuePattern[{patt1,}]

is a pattern object that represents an association or list of rules that includes elements matching each of the patti.

Details

  • The patti are often of the form keyivali but can be any pattern.
  • Elements matching the patti can appear in any order in the association or list of rules.
  • The patti are matched in the order they appear.
  • Each of the patti is taken to match a different element of the association or list of rules.
  • For a single pattern patt, KeyValuePattern[patt] is equivalent to KeyValuePattern[{patt}].
  • KeyValuePattern[{}] matches any association or list of rules.

Examples

open allclose all

Basic Examples  (3)

KeyValuePattern lets you match any element in an association:

It also works on lists of rules:

KeyValuePattern matches elements that appear anywhere in an association:

Match any association with two entries:

Scope  (6)

KeyValuePattern distinguishes Rule and RuleDelayed:

Extract the key from an Association that matches a rule:

Extract a rule based on a condition:

Every rule in Association is matched at most once:

A single pattern in KeyValuePattern does not need to be in a List:

An empty KeyValuePattern matches any association or list of rules:

Applications  (1)

Apply a filter to a list of associations:

This works similarly to the following:

Properties & Relations  (3)

KeyValuePattern matches in any order:

A pattern containing an Association matches rules in exact order:

An expression matched by KeyValuePattern may contain other rules besides those specified:

A pattern containing an Association will attempt to match against all rules:

BlankNullSequence in KeyValuePattern matches only a single rule:

In a pattern containing an Association, BlankNullSequence may match more than one rule:

Possible Issues  (4)

Every pattern in KeyValuePattern may match only one rule:

Empty list or Association can be matched by BlankNullSequence or RepeatedNull patterns:

KeyValuePattern matches only an Association or a list of rules, even if no patterns are specified:

KeyValuePattern will not match RuleDelayed if Rule is specified:

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

Text

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

CMS

Wolfram Language. 2016. "KeyValuePattern." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/KeyValuePattern.html.

APA

Wolfram Language. (2016). KeyValuePattern. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/KeyValuePattern.html

BibTeX

@misc{reference.wolfram_2024_keyvaluepattern, author="Wolfram Research", title="{KeyValuePattern}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/KeyValuePattern.html}", note=[Accessed: 11-December-2024 ]}

BibLaTeX

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