10
$\begingroup$

I have the following list which is built up of associations, that is,

z = {<|{"a", "b"} -> 1, {"c", "d"} -> 2, {"e", "f"} -> 
    3|>, <|{"g", "h"} -> 4, {"i", "j"} -> 5|>, <|{"k", "l"} -> 
    6, {"m", "n"} -> 7, {"o", "p"} -> 8, {"q", "r"} -> 
    9|>, <|{"s", "t"} -> 10|>}

I am trying to to pick the first elements in each association such that it gives:

 {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
 6|>, <|{"s", "t"} -> 10|>}

to do that I do,

Map[First, z]

but this only returns the values, how can one fix this?

$\endgroup$

4 Answers 4

13
$\begingroup$

You can use the fact that Part ([[…]]) preserves the keys when you supply a list of indices to extract:

z[[All, {1}]]
(* {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
   6|>, <|{"s", "t"} -> 10|>} *)

(* without the list *)
z[[All, 1]]
(* {1, 4, 6, 10} *)

Alternatively you could use Take:

Map[Take[#, 1] &, z]
(* {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
   6|>, <|{"s", "t"} -> 10|>} *)

Other possibilites:

Map[#[[{1}]] &, z]
(* {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
   6|>, <|{"s", "t"} -> 10|>} *)

z[[All, ;; 1]]
(* {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
   6|>, <|{"s", "t"} -> 10|>} *)

In essence, anything that could in principle return multiple elements will preserve the keys (or, more generally, will preserve that level of the nested expression)

$\endgroup$
7
$\begingroup$

For completeness sake, a few more:

Extract[z, {All, {1}}]
(* {<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 
   6|>, <|{"s", "t"} -> 10|>} *)

Cases[z, KeyValuePattern[{x_ -> y_}] :> x -> y]
(*{{"a", "b"} -> 1, {"g", "h"} -> 4, {"k", "l"} -> 6, {"s", "t"} -> 10}*)
$\endgroup$
3
$\begingroup$
z =
  {<|{"a", "b"} -> 1, {"c", "d"} -> 2, {"e", "f"} -> 3|>,
   <|{"g", "h"} -> 4, {"i", "j"} -> 5|>,
   <|{"k", "l"} -> 6, {"m", "n"} -> 7, {"o", "p"} -> 8, {"q", "r"} -> 9|>,
   <|{"s", "t"} -> 10|>};

1.

Using Query to get the 1st elements

Query[All, {1}] @ z

{<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 6|>, <|{"s", "t"} -> 10|>}

Values only

Query[Values, 1] @ z

{1, 4, 6, 10}

Keys only

Query[Keys, 1] @ z

{{"a", "b"}, {"g", "h"}, {"k", "l"}, {"s", "t"}}

2.

Using Query to get the 2nd elements

Query[All, {2}] @ z

{<|{"c", "d"} -> 2|>, <|{"i", "j"} -> 5|>, <|{"m", "n"} -> 7|>, Missing["PartAbsent", {2}]}

DeleteMissing[%]

{<|{"c", "d"} -> 2|>, <|{"i", "j"} -> 5|>, <|{"m", "n"} -> 7|>}

DeleteMissing @ Query[Values, 2] @ z

{2, 5, 7}

or

DeleteMissing @ Query[All, 2] @ z

{2, 5, 7}

DeleteMissing @ Query[Keys, 2] @ z

{{"c", "d"}, {"i", "j"}, {"m", "n"}}

$\endgroup$
1
$\begingroup$
z =
  {<|{"a", "b"} -> 1, {"c", "d"} -> 2, {"e", "f"} -> 3|>,
   <|{"g", "h"} -> 4, {"i", "j"} -> 5|>,
   <|{"k", "l"} -> 6, {"m", "n"} -> 7, {"o", "p"} -> 8, {"q", "r"} -> 9|>,
   <|{"s", "t"} -> 10|>};

A variant of sakra's answer using ReplaceAll:

z /. KeyValuePattern[x_ -> y_] :> <|x -> y|>

{<|{"a", "b"} -> 1|>, <|{"g", "h"} -> 4|>, <|{"k", "l"} -> 6|>, <|{"s", "t"} -> 10|>}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.