5

I have a list of the form:

toy = {{a, {b, c}}, {d, {e, f}}, {g, {h, j}}, {k, {m, n}}};

and would like to create the list

{{a, c}, {d, f}, {g, j}, {k, n}}

from toy.

The following code does it but I find this esthetically unsatisfactory because I don't like creating the two independent lists toy[[;; , 1]] and toy[[;; , 2, 2]] and combining them.

Transpose[{toy[[;; , 1]], toy[[;; , 2, 2]]}]

Is there a cleaner more direct way to do this?

JEP
  • 1,336
  • 1
  • 9
  • 20

8 Answers8

7

Is this what you want:

Map[{#[[1]], #[[2, 2]]} &, toy]
4

One should ban all those comments spammers one day... :)

To not duplicate them:

toy[[All , 2, 0]] = #2 &;
toy
{{a, c}, {d, f}, {g, j}, {k, n}}

Keep in mind that this modifies toy.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • +1 nice one :) How come you (and others) use ;; instead of All? – Mike Honeychurch Jan 27 '15 at 07:24
  • @MikeHoneychurch you mean why? 2 is less than 3 characters :) But may be obscuring sometimes and doesn't work for <V9 in every form. Still, I like it :) – Kuba Jan 27 '15 at 07:27
  • @Mike Probably to save a character, but even as a terse fanatic I prefer All. First because it is descriptive and ledible, and second because it is backward-compatible. (;; does not work in v7.) It's only one more character so I vote for All. I've even edited it into a few answers here and there. – Mr.Wizard Jan 27 '15 at 07:27
  • @Mr.Wizard ok ok... :) – Kuba Jan 27 '15 at 07:27
  • @Kuba Of course you have your own style and it doesn't have to agree with mine. :-) – Mr.Wizard Jan 27 '15 at 07:28
  • 1
    @Mr.Wizard I don't! My notebooks are full of ;; :) – Kuba Jan 27 '15 at 07:29
4

Maybe:

Extract[{{1}, {2, 2}}] /@ toy
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
3
☺ = {#, #2 & @@ #2} & @@@ # &;
☺ @ toy

{{a, c}, {d, f}, {g, j}, {k, n}}

kglr
  • 394,356
  • 18
  • 477
  • 896
3
(Flatten /@ toy)[[;; , {1, 3}]]
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
2
list = {{a, {b, c}}, {d, {e, f}}, {g, {h, j}}, {k, {m, n}}};

Using ReplaceAt (new in 13.1)

ReplaceAt[list, {_, a_} :> a, {;; , 2}]

{{a, c}, {d, f}, {g, j}, {k, n}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1

Using ReplaceList:

list = {{a, {b, c}}, {d, {e, f}}, {g, {h, j}}, {k, {m, n}}};

ReplaceList[#, {s1_, {s2__}} :> Splice@{s1, First@s2}] & /@ list

({{a, c}, {d, f}, {g, j}, {k, n}})

Or using Cases:

Cases[#, {s1_, {s2__}} :> {s1, First@s2}] &@list

({{a, c}, {d, f}, {g, j}, {k, n}})

Or using ReplaceAll:

# /. {s2_, s3 : {__}} :> {s2, Last@s3} &@list

({{a, c}, {d, f}, {g, j}, {k, n}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1
toy = {{a, {b, c}}, {d, {e, f}}, {g, {h, j}}, {k, {m, n}}};

Cases[toy, {a_, {b_, c_}} :> {a, c}]
{First@#, Last@Last@#} & /@ toy
Flatten@*ReplacePart[{{2, 1}} -> Nothing] /@ toy
Flatten@*Delete[{2, 1}] /@ toy
#[[{1, -1}]] &@*MapAt[Splice, 2] /@ toy
{First@#, Last@#} & /@ MapAt[Splice, toy, {All, 2}]
Through[{First, Last}[#]] & /@ MapAt[Splice, toy, {All, 2}]

Result:

{{a, c}, {d, f}, {g, j}, {k, n}}

Syed
  • 52,495
  • 4
  • 30
  • 85