Key
Details
- Key is used in functions such as Part to access values in Association objects or columns in Tabular objects.
- Key can normally be omitted when key is a string.
Examples
open all close allBasic Examples (2)
Extract a value from an association as a part:
<|a -> b, c -> d|>[[Key[a]]]Extract a value from an association by function application:
<|a -> b, c -> d|>[a]Extract a column from a Tabular object:
Tabular[{<|a -> 1, b -> 2, c -> 3|>, <|a -> 4, b -> 5, c -> 6|>}]%[[All, Key[b]]]Scope (6)
The Key wrapper is not necessary when the key is a string:
<|"a" -> b, "c" -> d|>[["a"]]Lookups in associations interoperate with parts in lists and other expressions:
{<|a -> x, b -> {y, z}|>, w}[[1, Key[b], 2]]The explicit Key can be omitted when the keys in an association are strings:
{<|"a" -> x, "b" -> z|>}[[1, "b"]]<|"a" -> b, "c" -> d|>["a"]Key is normally generated by functions such as Position:
Position[<|a -> 1, b -> 2|>, _Integer]Use Extract to extract the values:
Extract[<|a -> 1, b -> 2|>, %]For expressions at deeper levels, the positions can be combinations of integers and Key objects:
Position[{<|"a" -> 1, "b" -> <|"b1" -> 2, "b2" -> 3|>|>}, _ ? OddQ]Use Key to disambiguate between an integer index and an integer key:
assoc = <|2 -> "a", 1 -> "b"|>;assoc[[2]]assoc[[Key[2]]]assoc[[Key[key]]] is equivalent to assoc[key], including integer keys:
% === assoc[2]Take a Tabular object with several columns:
tab = Tabular[{{1, True, Today}, {2, False, Tomorrow}}, {"col", bool, "date"}]Columns with non-string keys require the explicit use of Key for Part extraction:
tab[[All, {"col", Key[bool]}]]String keys can be used with or without a Key wrapper:
tab[[All, {"col", Key["date"]}]]Repeated keys are treated as one, even if they are wrapped with Key:
tab[[All, {"date", Key["date"]}]]FromTabular[tab, "Dataset"]//NormalTake a Dataset object with several rows:
ds = Dataset[<|"col" -> 1, bool -> True, "date" -> DateObject[{2024, 11, 21}, "Day"]|>]Use Part to extract a row with a string key:
ds[["col"]]Alternatively, use ds[key], which is equivalent to Query[key][ds]:
ds["col"]Query["col"][ds]For non-string keys, extraction requires wrapping them with Key:
ds[[Key[bool]]]ds[Key[bool]]Otherwise, Query will interpret the key as a function to be used on the row:
ds[bool]Query[bool][ds]Applications (1)
Use Key in the Function body of Tabular transformations when the key is a general expression:
tab = ToTabular[{f[x] -> Power[Sin[x] ^ Range[5]]}, "Columns"]TransformColumns[tab, f'[x] -> Function[D[#[Key[f[x]]], x]]]TransformColumns[tab, f'[x] -> Function[D[Slot[Key[f[x]]], x]]]Properties & Relations (2)
Key always takes one argument:
key = Key["k"]ExtendedKey can take more than one argument:
ekey = ExtendedKey["a", "b", "c"]Both keys can then be used to address their values:
assoc = <|key -> "foo", ekey -> "bar"|>;assoc[key]assoc[ekey]Take a Tabular object with a key column:
tab = Tabular[{{"one", True, "foo"}, {"two", False, "bar"}}, <|"ColumnKeys" -> {"key", "col1", "col2"}, "KeyColumns" -> {"key"}|>]Key wrappers can be used to extract tabular rows and columns:
tab[Key["one"]]tab[[All, Key["col2"]]]Row extraction can also be performed with RowKey:
tab[RowKey["one"]]See Also
Related Guides
Text
Wolfram Research (2014), Key, Wolfram Language function, https://reference.wolfram.com/language/ref/Key.html (updated 2025).
CMS
Wolfram Language. 2014. "Key." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/Key.html.
APA
Wolfram Language. (2014). Key. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Key.html
BibTeX
@misc{reference.wolfram_2026_key, author="Wolfram Research", title="{Key}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/Key.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_key, organization={Wolfram Research}, title={Key}, year={2025}, url={https://reference.wolfram.com/language/ref/Key.html}, note=[Accessed: 12-June-2026]}