gathers into sublists each set of elements in list that gives the same value when f is applied.


gathers list into nested sublists using fi at level i.


  • GatherBy[{e1,e2,e3,},f] evaluates f[ei] for each i to determine how to gather the ei.
  • GatherBy[list,] gives results that follow the ordering of elements in list.
  • Within each sublist generated by GatherBy[list,], elements appear in the same order as in list; the complete sublists are ordered so that their first elements appear in the same order as in list.
  • GatherBy[list] is equivalent to GatherBy[list,Identity], which is also equivalent to Gather[list]. »
  • GatherBy effectively produces an equivalence partition.
  • GatherBy[list,{f1,f2}] is equivalent to Map[GatherBy[#,f2]&,GatherBy[list,f1]].


open allclose all

Basic Examples  (2)

Gather data in odd and even lists:

Gather by the first part:

Applications  (6)

Sort and then gather strings by their first letters:

Gather by the first component, and within that group, gather by the last component:

Use Framed to display grouping:

Gather by remainder:

Gather by the first component in a vector:

Gather dates by year:

Simulate some data:

Group elements based on the value of the second element:

Count the number of data points in each group:

Compute the means of the third element by group:

Group machine numbers by equivalence class based on rounding off the last tol bits:

Generate an unordered sample of distinct machine numbers close to 1:

Gather by equivalence class:

See how the factors in were grouped:

Here are the equivalence representatives and the values such that :

Properties & Relations  (2)

GatherBy[list] is equivalent to Gather[list]:

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:

Wolfram Research (2008), GatherBy, Wolfram Language function,


Wolfram Research (2008), GatherBy, Wolfram Language function,


Wolfram Language. 2008. "GatherBy." Wolfram Language & System Documentation Center. Wolfram Research.


Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2021_gatherby, author="Wolfram Research", title="{GatherBy}", year="2008", howpublished="\url{}", note=[Accessed: 16-May-2022 ]}


@online{reference.wolfram_2021_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2008}, url={}, note=[Accessed: 16-May-2022 ]}