Skip to content

Gather ​

Group identical elements into sublists, in order of first appearance.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageGather✓
Gather(list)elements grouped by equality, in first-appearance order.
Gather(list, test)elements grouped by a custom two-argument equivalence test.
Typed signatures
TypePackageApplies
(indexed_collection<T>, ((T, T) any -> boolean)?) -> list<list<T>> where Tcombinatoricsalways

Domain: Collections

Details
  • Groups, not runs: every occurrence of a value lands in the same group wherever it appears — unlike Split, which only joins ADJACENT equal elements. See GatherBy to group by a function's value instead of the elements themselves.

Examples

Basic #
live ↗
InGather([1,7,3,7,2,3,9])\mathrm{Gather}(\bigl\lbrack1, 7, 3, 7, 2, 3, 9\bigr\rbrack)Out[[1],[7,7],[3,3],[2],[9]]\bigl\lbrack\bigl\lbrack1\bigr\rbrack, \bigl\lbrack7, 7\bigr\rbrack, \bigl\lbrack3, 3\bigr\rbrack, \bigl\lbrack2\bigr\rbrack, \bigl\lbrack9\bigr\rbrack\bigr\rbrack

Symbolic elements

live ↗
InGather([a,b,a,a,c,b])\mathrm{Gather}(\bigl\lbrack a, b, a, a, c, b\bigr\rbrack)Out[[a,a,a],[b,b],[c]]\bigl\lbrack\bigl\lbrack a, a, a\bigr\rbrack, \bigl\lbrack b, b\bigr\rbrack, \bigl\lbrack c\bigr\rbrack\bigr\rbrack

Equal elements are grouped, with groups ordered by first appearance

live ↗
InGather([a,b,a,d,b])\mathrm{Gather}(\bigl\lbrack a, b, a, d, b\bigr\rbrack)Out[[a,a],[b,b],[d]]\bigl\lbrack\bigl\lbrack a, a\bigr\rbrack, \bigl\lbrack b, b\bigr\rbrack, \bigl\lbrack d\bigr\rbrack\bigr\rbrack

A custom equivalence groups pairs sharing a first component

live ↗
InGather([[a,1],[b,1],[a,2],[d,1],[b,3]],(_1⁡,_2⁡)↦First(_1⁡)=First(_2⁡))\mathrm{Gather}(\bigl\lbrack\bigl\lbrack a, 1\bigr\rbrack, \bigl\lbrack b, 1\bigr\rbrack, \bigl\lbrack a, 2\bigr\rbrack, \bigl\lbrack d, 1\bigr\rbrack, \bigl\lbrack b, 3\bigr\rbrack\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\mathrm{First}(\operatorname{\_1})=\mathrm{First}(\operatorname{\_2}))Out[[[a,1],[a,2]],[[b,1],[b,3]],[[d,1]]]\bigl\lbrack\bigl\lbrack\bigl\lbrack a, 1\bigr\rbrack, \bigl\lbrack a, 2\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack b, 1\bigr\rbrack, \bigl\lbrack b, 3\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack d, 1\bigr\rbrack\bigr\rbrack\bigr\rbrack
Scope #

A test decides which elements belong together: here, same parity

live ↗
InGather([1,2,3,4,5,6],(_1⁡,_2⁡)↦_1⁡ mod 2=_2⁡ mod 2)\mathrm{Gather}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}\bmod2=\operatorname{\_2}\bmod2)Out[[1,3,5],[2,4,6]]\bigl\lbrack\bigl\lbrack1, 3, 5\bigr\rbrack, \bigl\lbrack2, 4, 6\bigr\rbrack\bigr\rbrack
Applications #

Groups the integers 1..101..10 into residue classes mod 33

live ↗
InGather(1..10,(_1⁡,_2⁡)↦_1⁡ mod 3=_2⁡ mod 3)\mathrm{Gather}(1..10, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}\bmod3=\operatorname{\_2}\bmod3)Out[[1,4,7,10],[2,5,8],[3,6,9]]\bigl\lbrack\bigl\lbrack1, 4, 7, 10\bigr\rbrack, \bigl\lbrack2, 5, 8\bigr\rbrack, \bigl\lbrack3, 6, 9\bigr\rbrack\bigr\rbrack

Exact rationals are grouped by integer part, giving thirds in each unit interval

live ↗
InGather(0..(0+13)..3,(_1⁡,_2⁡)↦⌊_1⁡⌋=⌊_2⁡⌋)\mathrm{Gather}(0..(0+\frac{1}{3})..3, (\operatorname{\_1}, \operatorname{\_2})↦\lfloor\operatorname{\_1}\rfloor=\lfloor\operatorname{\_2}\rfloor)Out[[0,13,23],[1,43,53],[2,73,83],[3]]\bigl\lbrack\bigl\lbrack0, \frac{1}{3}, \frac{2}{3}\bigr\rbrack, \bigl\lbrack1, \frac{4}{3}, \frac{5}{3}\bigr\rbrack, \bigl\lbrack2, \frac{7}{3}, \frac{8}{3}\bigr\rbrack, \bigl\lbrack3\bigr\rbrack\bigr\rbrack
live ↗
InGather(data,test)\mathrm{Gather}(\mathrm{data}, \mathrm{test})OutGather(data,test)\mathrm{Gather}(\mathrm{data}, \mathrm{test})
Properties #

The group sizes are the multiplicities of the distinct elements

live ↗
InMap(Length,Gather([1,7,3,7,2,3,9]))=[1,2,2,1,1]\mathrm{Map}(\mathrm{Length}, \mathrm{Gather}(\bigl\lbrack1, 7, 3, 7, 2, 3, 9\bigr\rbrack))=\bigl\lbrack1, 2, 2, 1, 1\bigr\rbrackOutTrue\mathrm{True}
About
Examples9
Checked againstWolfram Language 9 agree
CrosswalkWolfram Language

See also: GatherBy, Split, Union