Mathematica > Core Language > List Manipulation > Rearranging & Restructuring Lists >
Mathematica > Data Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists >

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 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 effectively produces an equivalence partition.
  • GatherBy[list, {f1, f2}] is equivalent to Map[GatherBy[#, f2]&, GatherBy[list, f1]].
New in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team