The groupby()
function does exactly what its name says. Using groupby()
we can group values of an iterable together based on a certain condition. The one quirk of this function is that it expects the iterable to be in sorted order. Hence we need to sort our lists before we use them.
In the example below we are grouping names based on their first character.