Add a groupby method to dicts
This function behaves similar to Django's groupby filter.
Patch by: Sverre Rabbelier
--- a/app/soc/logic/dicts.py Tue Feb 17 06:52:06 2009 +0000
+++ b/app/soc/logic/dicts.py Tue Feb 17 14:25:40 2009 +0000
@@ -181,3 +181,19 @@
result = tmpresult
return result
+
+def groupby(target, group_key):
+ """Groups a list of dictionaries by group_key.
+ """
+
+ result = {}
+
+ for value in target:
+ key_value = value[group_key]
+
+ if not key_value in result:
+ result[key_value] = []
+
+ result[key_value].append(value)
+
+ return result