GroupBy

GroupBy[{elem1,elem2,},f]
gives an association that groups the elemi into lists associated with distinct keys f[elemi].

GroupBy[{elem1,elem2,},fkfv]
groups the fv[elemi] according to the fk[elemi].

GroupBy[{elem1,elem2,},{fs1,fs2,}]]
groups into nested associations using fsi at level i.

GroupBy[{elem1,elem2,},spec,red]
applies the function red to reduce lists of values that are generated.

GroupBy[spec]
represents an operator form of GroupBy that can be applied to an expression.

DetailsDetails

  • GroupBy provides a generalization of the map reduce operation.
  • GroupBy[list,f] gives an association whose keys are the distinct f[elemi] and whose values are sublists of the list list.
  • GroupBy[assoc,f] gives an association whose keys are the distinct f[elemi] and whose values are subassociations of the association assoc.
  • GroupBy[spec][expr] is equivalent to GroupBy[expr,spec].

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

Group pairs of items according to the first element of the pair:

In[1]:=
Click for copyable input
Out[1]=

Group values of an association:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 2014
(10.0)