Wolfram: List. Partition, Reshape, Split, Gather

By Xah Lee. Date: . Last updated: .

Function related to restructuring of list.

Partition (Split into Groups Evenly)

Partition
  • Partition[ list, n ]
  • Partition[ list, n, d ]
  • and more

group every n items into sublist, with overlap of d items. (default is 0)

Partition[ {1, 2, 3, 4, 5, 6}, 2 ]
 (* {{1, 2}, {3, 4}, {5, 6}} *)

(* with overlap *)
Partition[ {1, 2, 3, 4, 5, 6}, 2, 1 ]
(* {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}} *)

🛑 WARNING: unfit tail items are dropped.

(* unfit tail items are dropped *)
Partition[ {1, 2, 3, 4, 5, 6, 7}, 3 ]
(* {{1, 2, 3}, {4, 5, 6}} *)

ArrayReshape

ArrayReshape[ {1, 2, 3, 4, 5, 6}, {2,3} ]
(* {{1, 2, 3}, {4, 5, 6}} *)

ArrayReshape[ {1, 2, 3, 4, 5, 6}, {3,2} ]
(* {{1, 2}, {3, 4}, {5, 6}} *)

Split into Sublist When Neighbors Are Different

Split
  • Split[ list ]
  • Split[ list, testF ]
  • split a list into sublists at places were neighboring items are different, using testF.
  • testF takes 2 args and return True or false.
Split[ {2, 2, 3, 3, 3, 5} ]
(* {{2, 2}, {3, 3, 3}, {5}} *)

Split[
 {2, 2, 3, 3, 3, 5},
 Function[{x,y}, EvenQ[ x ] === EvenQ[ y ]]
 ]
(* {{2, 2}, {3, 3, 3, 5}} *)
SplitBy
  • SplitBy[ list, f ]
  • SplitBy[ list, {f1, f2, etc} ]
  • Split a list into sublists at places were neighboring items are different. Items are considered different if function f on them are different.
  • If given list of functions {f1, f2, etc}, use subsequent functions to break a tie.

💡 TIP: this is like Split, but more convenient to use for the predicate function to determine if two items are same, because the function just need to take a single argument.

SplitBy[ {2, 2, 3, 3, 3, 5}, OddQ ]
(* {{2, 2}, {3, 3, 3, 5}} *)

Group a List into Sublists Same Items

Gather
  • Gather[ list ]
  • Gather[ list, testF ]
  • group same items together into a sublist. (reorder if necessary.)
  • optionally by a function testF.
  • testF takes 2 args and return True or false.
Gather[ {5, 5, 8, 6, 5, 4} ]
(* {{5, 5, 5}, {8}, {6}, {4}} *)

Gather[
 {5, 5, 8, 6, 5, 4},
 Function[{x, y}, EvenQ[ x ] === EvenQ[ y ]] ]
(* {{5, 5, 5}, {8, 6, 4}} *)
GatherBy
  • GatherBy[ list, f ]
  • GatherBy[ list, {f1, f2, etc} ]
  • group same items together into sublists, reorder if necessary, by comparing the result of function f to each item.
  • f takes 1 arg and can return anything.
  • If given list of functions {f1, f2, etc}, use subsequent functions to refine.
(* group same items into sublists *)
GatherBy[ {5, 5, 8, 6, 5, 4}, Identity ]
(* {{5, 5, 5}, {8}, {6}, {4}} *)

(* group even and odd *)
GatherBy[ {5, 5, 8, 6, 5, 4}, EvenQ ]
(* {{5, 5, 5}, {8, 6, 4}} *)

(* group integers and non integers *)
GatherBy[ {5, 5.2, 8.6, 5, 4}, IntegerQ ]
(* {{5, 5, 4}, {5.2, 8.6}} *)

(* group by length *)
GatherBy[ {5, 5, {8, 3}, 5, {4, 2}}, Length ]
(* {{5, 5, 5}, {{8, 3}, {4, 2}}} *)

(* group by checking is factor of 3 *)
GatherBy[
 {30, 752, 333, 298, 903},
 Function[{x}, Mod[ x, 3 ] == 0 ]
 ]
(* {{30, 333, 903}, {752, 298}} *)

Wolfram. List Operations, and Loop, Iteration, Recursion