# HG changeset patch # User Sverre Rabbelier # Date 1236524431 0 # Node ID a0ae6e6431a9db46900585e35be7e3b3c2e73e3c # Parent f789ffe213a3cf0d6bb3c8ed3db7ef2946f70fb6 Add a groupDictBy to dicts Patch by: Sverre Rabbelier diff -r f789ffe213a3 -r a0ae6e6431a9 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)