Add a groupDictBy to dicts
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 08 Mar 2009 15:00:31 +0000
changeset 1749 a0ae6e6431a9
parent 1748 f789ffe213a3
child 1750 1ac2d27fdb6b
Add a groupDictBy to dicts Patch by: Sverre Rabbelier
app/soc/logic/dicts.py
--- a/app/soc/logic/dicts.py	Sun Mar 08 15:00:08 2009 +0000
+++ b/app/soc/logic/dicts.py	Sun Mar 08 15:00:31 2009 +0000
@@ -197,3 +197,13 @@
     result[key_value].append(value)
 
   return result
+
+def groupDictBy(target, key, new_key=None):
+  """Groups a dictionary by a key.
+  """
+
+  if not new_key:
+    new_key = key
+
+  result = ((k, v[new_key]) for k, v in target.iteritems() if v[key])
+  return dict(result)