KeyTake
Details
- KeyTake[assoc,…] gives an Association object whose elements are in the order of the keyi.
- Any keyi that does not appear in assoc is ignored.
- KeyTake can be applied not only to Association objects, but also to lists of rules. It can also be applied to lists of associations and lists of lists of rules, as well as Tabular and Dataset objects.
- KeyTake[keys][expr] is equivalent to KeyTake[expr,keys].
Examples
open all close allBasic Examples (4)
Select rules from an association, given a list of keys:
KeyTake[<|a -> 1, b -> 2, c -> 3|>, {a, b}]Select from several associations, given a list of keys:
KeyTake[{<|a -> 1, b -> 2|>, <|b -> 2, c -> 3|>}, {a, b}]Select from an association, using a single key:
KeyTake[<|a -> b, c -> d, b -> c|>, c]Use the operator form of KeyTake:
KeyTake[{a, e}][<|a -> b, c -> d, e -> f, g -> h|>]Scope (17)
Association (6)
Take the key-value pairs of an association having the given keys:
KeyTake[<|"a" -> α, "b" -> β, "c" -> γ|>, {"a", "c"}]Take a single key-value pair from an association:
KeyTake[<|"a" -> 10, "b" -> 20, "c" -> 30|>, {"b"}]Equivalently, give the key directly:
KeyTake[<|"a" -> 10, "b" -> 20, "c" -> 30|>, "b"]Keys not present in the association will be ignored:
KeyTake[<|"a" -> α, "b" -> β, "c" -> γ|>, {"a", "d"}]KeyTake[<|"a" -> α, "b" -> β, "c" -> γ|>, "d"]KeyTake[<|"a" -> 1.5, "b" -> 2.7, "c" -> 3.4|>, {}]Use the operator form of KeyTake:
KeyTake[{"a", "c"}][<|"a" -> α, "b" -> β, "c" -> γ|>]KeyTake["b"][<|"a" -> α, "b" -> β, "c" -> γ|>]Take key-value pairs from several associations in a list:
assocs = {<|"a" -> A1, "b" -> B1, "c" -> C1|>, <|"a" -> A2, "b" -> B2|>};KeyTake[assocs, {"a", "c"}]This is equivalent to mapping over the list of associations:
Map[KeyTake[{"a", "c"}], assocs]List of Rules (7)
Take the rules having the given keys:
KeyTake[{"a" -> α, "b" -> β, "c" -> γ}, {"a", "c"}]Take a single rule from a list of rules:
KeyTake[{"a" -> 10, "b" -> 20, "c" -> 30}, {"b"}]Equivalently, give the key directly:
KeyTake[{"a" -> 10, "b" -> 20, "c" -> 30}, "b"]Keys not present in the list of rules will be ignored:
KeyTake[{"a" -> α, "b" -> β, "c" -> γ}, {"a", "d"}]KeyTake[{"a" -> α, "b" -> β, "c" -> γ}, "d"]KeyTake[{"a" -> 1.5, "b" -> 2.7, "c" -> 3.4}, {}]Use the operator form of KeyTake:
KeyTake[{"a", "c"}][{"a" -> α, "b" -> β, "c" -> γ}]KeyTake["b"][{"a" -> α, "b" -> β, "c" -> γ}]Take rules from several lists of rules:
lists = {{"a" -> A1, "b" -> B1, "c" -> C1}, {"a" -> A2, "b" -> B2}};KeyTake[lists, {"a", "c"}]This is equivalent to mapping over the list of lists of rules:
Map[KeyTake[{"a", "c"}], lists]KeyTake can act on lists containing both associations and lists of rules, returning a list of associations:
data = {<|"a" -> A1, "b" -> B1, "c" -> C1|>, {"a" -> A2, "b" -> B2}};KeyTake[data, {"a", "c"}]Tabular (2)
This is a Tabular object with three columns:
tab = Tabular[{{1, 11, True}, {2, 11, False}, {3, 13, False}}, {"a", "b", "c"}]Form another Tabular object by taking the specified columns:
KeyTake[tab, {"a", "c"}]KeyTake[tab, "b"]Order of keys is preserved in the output:
KeyTake[tab, {"c", "a"}]Extract an object with zero columns:
KeyTake[tab, {}]%//NormalConstruct a Tabular object with extended column keys:
tab = Tabular[{{1, 11, True, False}, {2, 11, False, True}, {3, 13, False, False}}, {ExtendedKey["int", "a"], ExtendedKey["int", "b"], ExtendedKey["bool", "a"], ExtendedKey["bool", "c"]}]KeyTake[tab, {ExtendedKey["int", "a"], ExtendedKey["bool", "a"]}]The key "c" uniquely identifies a column:
KeyTake[tab, "c"]The key "a" does not uniquely identify a column, and therefore the result is empty:
KeyTake[tab, "a"]Dataset (2)
Take a Dataset object of depth 2 with three columns:
ds = Dataset[{<|"a" -> 1, "b" -> 11, "c" -> True|>, <|"a" -> 2, "b" -> 11, "c" -> False|>, <|"a" -> 3, "b" -> 13, "c" -> False|>}]Extract another Dataset object with only the specified columns:
KeyTake[ds, {"a", "c"}]KeyTake[ds, "b"]Extract an object with zero columns:
KeyTake[ds, {}]%//NormalKeyTake can act on Dataset objects of depth 1 whose normal form is an association:
Dataset[<|"a" -> α, "b" -> β, "c" -> γ|>]KeyTake[%, {"a", "c"}]Properties & Relations (3)
The order of the provided keys is preserved in the result:
KeyTake[<|a -> 1, b -> 2, c -> 3|>, {a, b}]KeyTake[<|a -> 1, b -> 2, c -> 3|>, {c, a}]KeyTake returns a result with the given keys:
KeyTake[<|a -> 1, b -> 2, c -> 3|>, {a, b}]KeyDrop returns a result with all other keys:
KeyDrop[<|a -> 1, b -> 2, c -> 3|>, {a, b}]An alternative form for KeyTake[data,{key1,…}] is KeySelect[data,MatchQ[key1|…]]:
KeyTake[<|a -> 1, b -> 2, c -> 3|>, {a, b}]KeySelect[<|a -> 1, b -> 2, c -> 3|>, MatchQ[a | b]]Related Guides
Text
Wolfram Research (2014), KeyTake, Wolfram Language function, https://reference.wolfram.com/language/ref/KeyTake.html (updated 2025).
CMS
Wolfram Language. 2014. "KeyTake." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/KeyTake.html.
APA
Wolfram Language. (2014). KeyTake. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/KeyTake.html
BibTeX
@misc{reference.wolfram_2026_keytake, author="Wolfram Research", title="{KeyTake}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/KeyTake.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_keytake, organization={Wolfram Research}, title={KeyTake}, year={2025}, url={https://reference.wolfram.com/language/ref/KeyTake.html}, note=[Accessed: 13-June-2026]}