1
$\begingroup$

I know riffle will combine the lists {1,2,3} and {a,b,c} to be {1,a,2,b,3,c}. Is there a way to work backward and split the list {a,b,c,d,e,f} to be {a,c,e} and {b,d,f}?

$\endgroup$
6
  • 2
    $\begingroup$ {a,b,c,d,e,f}[[#;;;;2]]&/@{1,2} $\endgroup$ Commented Jun 2, 2014 at 13:51
  • 2
    $\begingroup$ Transpose[Partition[{a, b, c, d, e, f}, 2]] $\endgroup$ Commented Jun 2, 2014 at 13:54
  • $\begingroup$ @belisarius (& nikie) Why don't you post an answer? $\endgroup$
    – Szabolcs
    Commented Jun 2, 2014 at 13:54
  • $\begingroup$ @Szabolcs b/c I don't fully understand the question. Specifically, what has Riffle[] to do with Part[]? $\endgroup$ Commented Jun 2, 2014 at 13:56
  • $\begingroup$ @belisarius As I understand it (based on the example, which seems clear), yours is a good solution. Get back x and y from the result of Riffle[x,y], provided that x and y are of the same length. $\endgroup$
    – Szabolcs
    Commented Jun 2, 2014 at 13:58

2 Answers 2

6
$\begingroup$

You may want to try

ClearAll[unRiffle];
unRiffle[{}] = {{}, {}};
unRiffle[x_List] := x[[# ;; ;; 2]] & /@ {1, 2}
{lOdd, lEven} = unRiffle[{a, b, c, d, e, f}]
(*
 {{a, c, e}, {b, d, f}}
*)
$\endgroup$
1
  • 2
    $\begingroup$ Bad alternative: Rest@Extract[list, {{}, {;; ;; 2}, {2 ;; ;; 2}}] $\endgroup$ Commented Jun 2, 2014 at 14:18
4
$\begingroup$

Just for fun:

ClearAll[unRiffle];
unRiffle[{a_, l___}, {x___}, y_] := unRiffle[{l}, y, {x, a}];
unRiffle[{}, x_, y_] := {x, y};
unRiffle[l_List] := unRiffle[l, {}, {}];
$\endgroup$

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