Merging two dictionaries

Chris Rebert clp2 at
Mon Aug 2 09:26:54 CEST 2010

On Mon, Aug 2, 2010 at 12:06 AM, Douglas Garstang
<doug.garstang at> wrote:
> Actually, I had issues with trying recurse through the structures in
> tandem too. This didn't work:
> for a,b,c,d in ( cluster.iteritems(), default.iteritems() ):
>    ... do something ...
> It returns an unpack error.

Well, yeah. That for-loop has several problems:
- You're iterating over the items of a 2-tuple. It's just like:
for a,b,c,d in [1, 2]:
It's not treated any differently just because the items happen to be
iterators themselves. The iterators aren't automagically iterated
through in parallel just by putting them in a tuple. That would
require a zip().
- iteritems() returns a sequence of 2-tuples. Even when zipped, these
tuples don't get magically unpacked and repacked into 4-tuples:
for a, b, c, d in zip([(1,2), (3,4)], [(5,6), (7,8)]):
# still fails; can't unpack 2 separate tuples (i.e. (1,2) (5,6) )
directly into 4 variables; the nesting is wrong
- iteritems() returns the keys in an arbitrary order; the two
iteritems() calls won't be in any way "synchronized" so the keys match


More information about the Python-list mailing list