WolframLang: List Restructure Functions

By Xah Lee. Date: . Last updated: .

Function related to restructuring of list.

Grouping, Partition, Split, into Sublists

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

Partition[ {1, 2, 3, 4, 5, 6}, 2 ] ===
 {{1, 2}, {3, 4}, {5, 6}}

(* with overlab *)
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}} *)
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

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

SplitBy[ {2, 2, 3, 3, 3, 5}, OddQ ] ===
{{2, 2}, {3, 3, 3, 5}}
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

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.

GatherBy

(* 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}}

Transpose

Transpose
  • Transpose[list]
  • Transpose[list, m <-> n]
  • Transpose[list,{n1, n2, etc}]
  • Transpose a matrix. (swap the first and second index)
  • Transposes levels m and n. (default 1 and 2)
  • when given a list {n1, n2, etc}, transposes so that the k(th) level in list is the n(th) level in the result. (the list is a permutation of 1 to k, where k is ArrayDepth)

Transpose

Transpose[ {{a, b, c}, {1, 2, 3}} ] ===
{{a, 1}, {b, 2}, {c, 3}}

(* same as *)
Transpose[ {{a, b, c}, {1, 2, 3}}, {2, 1} ] ===
{{a, 1}, {b, 2}, {c, 3}}

(* same as *)
Transpose[ {{a, b, c}, {1, 2, 3}}, 1 <-> 2 ] ===
{{a, 1}, {b, 2}, {c, 3}}

(* HHHH--------------------------------------------------- *)

(* generalized transpose.
swapping positions of a list of indexes with its permutation *)

xx = Table[ f[x, y, z], {x, 1, 3}, {y, 1, 2}, {z, 1, 2}];

xx ===
{
{{f[1, 1, 1], f[1, 1, 2]}, {f[1, 2, 1], f[1, 2, 2]}},
{{f[2, 1, 1], f[2, 1, 2]}, {f[2, 2, 1], f[2, 2, 2]}},
{{f[3, 1, 1], f[3, 1, 2]}, {f[3, 2, 1], f[3, 2, 2]}}}

Transpose[ xx, {1, 3, 2} ] ===
{
{{f[1, 1, 1], f[1, 2, 1]}, {f[1, 1, 2], f[1, 2, 2]}},
{{f[2, 1, 1], f[2, 2, 1]}, {f[2, 1, 2], f[2, 2, 2]}},
{{f[3, 1, 1], f[3, 2, 1]}, {f[3, 1, 2], f[3, 2, 2]}}}

Flatten

Flatten
  • Flatten[ list]
  • Flatten[ list, n]
  • Flatten[ list, n, h ]

flatten a list, up to level n (default Infinity), and only those with Head h (default List)

Flatten

xx = {3, {9, {4}}, 5};

Flatten[ xx ] === {3, 9, 4, 5}

Flatten[ xx, 1 ] === {3, 9, {4}, 5}
Flatten[ xx, 2 ] === {3, 9, 4, 5}

(* flatten only those with head f *)
Flatten[ {3, f[9, {4}], g[5]}, Infinity, f ] ===
{3, 9, {4}, g[5]}
Riffle
  • Riffle[ list, x ]
  • Riffle[ list, x, n ]
  • Riffle[ list, {x1, x2, etc} ]

intersperse x into the list, at every nth (default 2), or use a list of values {x1, x2, etc}

Riffle

Riffle[ {1, 2, 3}, x ] ===
{1, x, 2, x, 3}

Riffle[ {1, 2, 3}, x, 2 ] ===
{1, x, 2, x, 3}

Riffle[ {1, 2, 3, 4, 5, 6}, x, 3 ] ===
{1, 2, x, 3, 4, x, 5, 6}

Riffle[ {1, 2, 3, 4, 5, 6}, x, 3 ] ===
{1, 2, x, 3, 4, x, 5, 6}

Riffle[ {1, 2, 3}, {a, b, c} ] ===
{1, a, 2, b, 3, c}
PadLeft

PadLeft

List Operations