1
$\begingroup$

Consider a list such as

list=Range[20]

{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }

I would like to have a function (e.g. fun[list_,chunks_]) that would cut it into two different lists, containing equal sized alternating chunks of the original list. For example:

{list1,list2}=fun[list,5];
list1
list2

{ 1, 2, 3, 4, 5, 11, 12, 13, 14, 15 }

{ 6, 7, 8, 9, 10, 16, 17, 18, 19, 20 }

Is there a function like that in mathematica? Or maybe one can implement it conveniently?

EDIT:

Here is my rough and ugly abomination

fun[list_, chunk_] := Block[{prt},
  If[! IntegerQ[Length[list]/chunk], Return["GTFO!:P"];];
  prt = Partition[list, chunk];
  {Flatten@prt[[1 ;; ;; 2]], Flatten@prt[[2 ;; ;; 2]]}
  ]

Certainly this should work much more elegantly/efficiently?

$\endgroup$

marked as duplicate by C. E., kglr list-manipulation Aug 28 at 21:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$

Maybe a bit simpler:

fun[list_,chunk_]:=
Flatten[Partition[list,chunk][[#;;;;2]]]&/@{1,2}
$\endgroup$
  • $\begingroup$ See also: answer. $\endgroup$ – C. E. Aug 28 at 21:17

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