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:

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  (3)

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:

Introduced in 2016
 (10.4)