Skip to main content
added 336 characters in body
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94

GatherBy theMethod 1: GatherBy each element's position (even or odd).

GatherBy[lst, Mod[Position[lst,#],2]&]

{{a, c, e, g}, {b, d, f}}


Method 2: Using ArrayReshape (version 9 only).

Mathematica graphics

In the following, the MathematicaIcon is used for padding in the reshaping of the array. After the array is reshaped, the icons are removed. Any element can be used in lieu of the MathematicaIcon, provide that one is certain that the padding element is not in the original list.

 ArrayReshape[lst,{Length[lst],2},"\[MathematicaIcon]"]\[Transpose]
 /."\[MathematicaIcon]"->Sequence[]

UsingMethod 3: Check whether each index from MapIndexed, is even or odd.

GatherBy[MapIndexed[List,lst],OddQ]/.{x_,{_}}:> x

GatherBy the element's position (even or odd).

GatherBy[lst, Mod[Position[lst,#],2]&]

{{a, c, e, g}, {b, d, f}}


Using ArrayReshape (version 9 only),

 ArrayReshape[lst,{Length[lst],2},"\[MathematicaIcon]"]\[Transpose]
 /."\[MathematicaIcon]"->Sequence[]

Using MapIndexed,

GatherBy[MapIndexed[List,lst],OddQ]/.{x_,{_}}:> x

Method 1: GatherBy each element's position (even or odd).

GatherBy[lst, Mod[Position[lst,#],2]&]

{{a, c, e, g}, {b, d, f}}


Method 2: Using ArrayReshape (version 9).

Mathematica graphics

In the following, the MathematicaIcon is used for padding in the reshaping of the array. After the array is reshaped, the icons are removed. Any element can be used in lieu of the MathematicaIcon, provide that one is certain that the padding element is not in the original list.

 ArrayReshape[lst,{Length[lst],2},"\[MathematicaIcon]"]\[Transpose]
 /."\[MathematicaIcon]"->Sequence[]

Method 3: Check whether each index from MapIndexed is even or odd.

GatherBy[MapIndexed[List,lst],OddQ]/.{x_,{_}}:> x
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94

GatherBy the element's position (even or odd).

GatherBy[lst, Mod[Position[lst,#],2]&]

{{a, c, e, g}, {b, d, f}}


Using ArrayReshape (version 9 only),

 ArrayReshape[lst,{Length[lst],2},"\[MathematicaIcon]"]\[Transpose]
 /."\[MathematicaIcon]"->Sequence[]

Using MapIndexed,

GatherBy[MapIndexed[List,lst],OddQ]/.{x_,{_}}:> x