5
$\begingroup$

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?

$\endgroup$
7
  • 4
    $\begingroup$ For ex.{#[[1]], #[[2, 2]]} & /@ toy or {#1, #2[[2]]} & @@@ toy $\endgroup$ Jan 27, 2015 at 4:49
  • 3
    $\begingroup$ toy /. {x_, {y_, z_}} :> {x, z} $\endgroup$
    – BoLe
    Jan 27, 2015 at 4:50
  • 3
    $\begingroup$ Just for something different: toy[[All, 2]] = toy[[All, 2, 2]]; toy $\endgroup$ Jan 27, 2015 at 4:56
  • $\begingroup$ Without destroying the toy Transpose@{toy[[All,1]],toy[[All,2,2]]} $\endgroup$
    – tchronis
    Jan 27, 2015 at 7:21
  • $\begingroup$ @tchronis That method is already in the question as "unsatisfactory" $\endgroup$
    – Mr.Wizard
    Jan 27, 2015 at 7:25

5 Answers 5

7
$\begingroup$

Is this what you want:

Map[{#[[1]], #[[2, 2]]} &, toy]
$\endgroup$
0
4
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ +1 nice one :) How come you (and others) use ` ;; ` instead of All? $\endgroup$ Jan 27, 2015 at 7:24
  • $\begingroup$ @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 :) $\endgroup$
    – Kuba
    Jan 27, 2015 at 7:27
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jan 27, 2015 at 7:27
  • $\begingroup$ @Mr.Wizard ok ok... :) $\endgroup$
    – Kuba
    Jan 27, 2015 at 7:27
  • 1
    $\begingroup$ @Mr.Wizard I don't! My notebooks are full of ;; :) $\endgroup$
    – Kuba
    Jan 27, 2015 at 7:29
4
$\begingroup$

Maybe:

Extract[{{1}, {2, 2}}] /@ toy
$\endgroup$
3
$\begingroup$
(Flatten /@ toy)[[;; , {1, 3}]]
$\endgroup$
3
$\begingroup$
☺ = {#, #2 & @@ #2} & @@@ # &;
☺ @ toy

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

$\endgroup$
2
  • 1
    $\begingroup$ Or, {#, Last@#2} & @@@ toy $\endgroup$
    – bbgodfrey
    Jan 20, 2018 at 1:02
  • 1
    $\begingroup$ @bbgodfrey, yes but then we can't say "look ma, no letters!" $\endgroup$
    – kglr
    Jan 20, 2018 at 2:33

Your Answer

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

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