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
  • 5
    $\begingroup$ For ex.{#[[1]], #[[2, 2]]} & /@ toy or {#1, #2[[2]]} & @@@ toy $\endgroup$ Jan 27, 2015 at 4:49
  • 4
    $\begingroup$ toy /. {x_, {y_, z_}} :> {x, z} $\endgroup$
    – BoLe
    Jan 27, 2015 at 4:50
  • 4
    $\begingroup$ Just for something different: toy[[All, 2]] = toy[[All, 2, 2]]; toy $\endgroup$ Jan 27, 2015 at 4:56
  • 1
    $\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

8 Answers 8

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
  • 2
    $\begingroup$ @bbgodfrey, yes but then we can't say "look ma, no letters!" $\endgroup$
    – kglr
    Jan 20, 2018 at 2:33
2
$\begingroup$
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}}

$\endgroup$
1
$\begingroup$
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}}

$\endgroup$
1
$\begingroup$

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}}*)
$\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.