http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
I can think of a few ways of doing it with loops but I'd particularly like to know if there is a neat one-liner.
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
I can think of a few ways of doing it with loops but I'd particularly like to know if there is a neat one-liner.
If the source of data (
people
in this case) is already sorted by the key, you can bypass thesorted
call:Note: If
sorted
is a common dictionary, there are no guarantees of order; therefore you must callsorted
. Here I'm supposing thatsorted
is acollections.OrderedDict
or some other kind of ordered data structure.Combine
itertools.groupby
withoperator.itemgetter
to get a pretty nice solution:Previous answers helped me to solve my problem. For future reference, if you have some nested data like
and you want to group by City and then by Company in that city like:
I solved it by doing this: