Gather

Gather[list]

gathers the elements of list into sublists of identical elements.

Gather[list,test]

applies test to pairs of elements to determine if they should be considered identical.

Details

  • The default function used to test whether elements are identical is SameQ.
  • Gather[{e1,e2,},p] will gather together elements for which p[ei,ej] returns True.

Examples

open allclose all

Basic Examples  (2)

Gather elements into sublists of identical elements:

Gather elements that have identical first parts:

Applications  (3)

Gather elements that have equal integer parts:

Gather integers that have identical remainders:

Gather polynomials that have identical remainders:

Properties & Relations  (2)

DeleteDuplicates[list] is equivalent to Gather[list][[All,1]]:

For a large list, DeleteDuplicates is much faster:

GatherBy[list,f] is equivalent to Gather[list,(f[#1]===f[#2])&]:

Gather the rows by Norm and display with the common norm of each group:

Compare to the result of Gather:

Possible Issues  (1)

When the test cannot be identified as an equality comparison, Gather may be much slower:

The results are identical:

Introduced in 2008
 (7.0)