From ba3fcb6b6fe68e96b7d4a9505ef28bc81d488709 Mon Sep 17 00:00:00 2001 From: wirawan Date: Fri, 19 Feb 2010 18:42:15 +0000 Subject: [PATCH] * Added: dict_join to join several dicts, with keys from latter dicts taking higher precedence. --- sugar.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sugar.py b/sugar.py index cd9b71f..7963b6d 100644 --- a/sugar.py +++ b/sugar.py @@ -1,6 +1,6 @@ #!/usr/bin/ipython -pylab # -# $Id: sugar.py,v 1.2 2010-02-08 19:58:44 wirawan Exp $ +# $Id: sugar.py,v 1.3 2010-02-19 18:42:15 wirawan Exp $ # # Created: 20100121 # Wirawan Purwanto @@ -52,3 +52,14 @@ def dict_slice(Dict, *keys): """ return dict([ (k, Dict[k]) for k in keys ]) +def dict_join(*dicts): + """Join multiple dicts into one, updating duplicate keys from left-to-right + manner. + Thus the key from the rightmost dict will take precedence.""" + + rslt = {} + for d in dicts: + rslt.update(d) + return rslt + +