BUILT-IN MATHEMATICA SYMBOL

# GatherBy

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

GatherBy[list, {f1, f2, ...}]
gathers list into nested sublists using at level i.

## DetailsDetails

• GatherBy[{e1, e2, e3, ...}, f] evaluates for each i to determine how to gather the .
• 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]].

## ExamplesExamplesopen allclose all

### Basic Examples (5)Basic Examples (5)

Gather data in odd and even lists:

 Out[1]=

Gather by remainder:

 Out[1]=

Gather by the first component in a vector:

 Out[1]=
 Out[2]=

Gather dates by year:

 Out[1]=
 Out[2]=

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

 Out[1]=
 Out[2]=

Use Framed to display grouping:

 Out[3]=

### Properties & Relations (2)Properties & Relations (2)

New in 7
New to Mathematica? Find your learning path »
Have a question? Ask support »