grouping things the groupby function is handy for grouping things it breaks list up into groups by tracing through the list and every time there is change in valuesa new group is created the groupby function returns ordered pairs that consist of list item and groupby iterator object that contains the group of items [ for key,group in groupby( )print(key''list(group) [ [ [ [ [ notice that we get two groups of zeros this is because groupby returns new group each time there is change in the list in the above exampleif we instead just wanted to know how many of each number occurwe can first sort the list and then call groupby [ sort(for key,group in groupby( )print(key''len(list(group)) most of the timeyou will want to sort your data before calling groupby optional argument the groupby function takes an optional argument that is function telling it how to group things when using thisyou usually have to first sort the list with that function as the sorting key here is an example that groups list of words by lengthl 'this ''is '' ''test ''of ''groupby ' sort(key lenfor key,group in groupby(llen)print(key''list(group) ' ['is''of' ['test''this' ['groupby'examples we close this section with two examples