GatherBy
✖
GatherBy
Details

- 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 effectively produces an equivalence partition.
- GatherBy[list,{f1,f2}] is equivalent to Map[GatherBy[#,f2]&,GatherBy[list,f1]].
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Applications (6)Sample problems that can be solved with this function
Sort and then gather strings by their first letters:

https://wolfram.com/xid/0btn96qzowjx-ddfp0t

https://wolfram.com/xid/0btn96qzowjx-fnd0dn

https://wolfram.com/xid/0btn96qzowjx-ezcoz3

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

https://wolfram.com/xid/0btn96qzowjx-i2ebc6


https://wolfram.com/xid/0btn96qzowjx-ess4m1

Use Framed to display grouping:

https://wolfram.com/xid/0btn96qzowjx-jb5a2m


https://wolfram.com/xid/0btn96qzowjx-g8y2tg

Gather by the first component in a vector:

https://wolfram.com/xid/0btn96qzowjx-pd3dn


https://wolfram.com/xid/0btn96qzowjx-pj6s1l


https://wolfram.com/xid/0btn96qzowjx-iw5ib


https://wolfram.com/xid/0btn96qzowjx-cg3l96


https://wolfram.com/xid/0btn96qzowjx-x1zngv

Group elements based on the value of the second element:

https://wolfram.com/xid/0btn96qzowjx-ojjuc0

Count the number of data points in each group:

https://wolfram.com/xid/0btn96qzowjx-biw2ln

Compute the means of the third element by group:

https://wolfram.com/xid/0btn96qzowjx-q4muwn

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

https://wolfram.com/xid/0btn96qzowjx-xj2b34
Generate an unordered sample of distinct machine numbers close to 1:

https://wolfram.com/xid/0btn96qzowjx-4mx5q8


https://wolfram.com/xid/0btn96qzowjx-xqs7nz

See how the factors in
were grouped:

https://wolfram.com/xid/0btn96qzowjx-wvgkl9

Here are the equivalence representatives and the values
such that
:

https://wolfram.com/xid/0btn96qzowjx-bmjigr

Properties & Relations (1)Properties of the function, and connections to other functions
GatherBy[list,f] is equivalent to Gather[list,(f[#1]===f[#2])&]:

https://wolfram.com/xid/0btn96qzowjx-glc9v8

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

https://wolfram.com/xid/0btn96qzowjx-k9tylk

Compare to the result of Gather:

https://wolfram.com/xid/0btn96qzowjx-p5iq4

Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.
Text
Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.
Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.
CMS
Wolfram Language. 2008. "GatherBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/GatherBy.html.
Wolfram Language. 2008. "GatherBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/GatherBy.html.
APA
Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GatherBy.html
Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GatherBy.html
BibTeX
@misc{reference.wolfram_2025_gatherby, author="Wolfram Research", title="{GatherBy}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/GatherBy.html}", note=[Accessed: 07-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2008}, url={https://reference.wolfram.com/language/ref/GatherBy.html}, note=[Accessed: 07-June-2025
]}