tags:

views:

122

answers:

6

EDIT

i have:

[{'a':1},{'b':2},{'c':1},{'d':2}]

the output should be:

{'a':1,'b':2,'c':1,'d':2}
+1  A: 
dict1.update( dict2 )

This is asymmetrical because you need to choose what to do with duplicate keys; in this case, dict2 will overwrite dict1. Exchange them for the other way.

EDIT: Ah, sorry, didn't see that.

It is possible to do this in a single expression:

>>> from itertools import chain
>>> dict( chain( *map( dict.items, theDicts ) ) )
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

No credit to me for this last!

However, I'd argue that it might be more Pythonic (explicit > implicit, flat > nested ) to do this with a simple for loop. YMMV.

katrielalex
its a list and not a dict:>>> type([{'a':1},{'b':2}]) <type 'list'>
killown
A: 

see this http://stackoverflow.com/questions/2365921/merging-python-dictionaries

shox
True, although that merges into a dict of lists.
katrielalex
it's a list and not a dict: >>> type([{'a':1},{'b':2}]) <type 'list'>
killown
+1  A: 
>>> L=[{'a':1},{'b':2},{'c':1},{'d':2}]    
>>> dict(i.items()[0] for i in L)
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

Note: the order of 'b' and 'c' doesn't match your output because dicts are unordered

if the dicts can have more than one key/value

>>> dict(j for i in L for j in i.items())
gnibbler
Fails if any of the dicts in the list has more than one item.
Paul McGuire
*cough*(assuming each dictionary contains only one key-value pair)*cough*
katrielalex
Ah, well first I posted a method that works with general dicts, but it looked like the OP didn't need that
gnibbler
+4  A: 

This works for dictionaries of any length:

>>> result = {}
>>> for d in L: result.update(d)
... 
{'a':1,'c':2,'b':1,'d':2}

And as generator-oneliner:

dict((k,v) for d in L for (k,v) in d.items())

Edit: As pointed out by katrielalex, dictionary comprehension:

{ k: v for d in L for k, v in d.items() }

is even better. OTOH they are Python3-only, so they may not be available to you.

delnan
Great minds think in parallel - you sir are brilliant!
Paul McGuire
@Paul McGuire: I sincerely hope appropriate use of for-loops, generator expressions and builtins is not considered genius (or in any way an exceptional skill) among Python programmers. Thanks anyway :)
delnan
Or even: `{ k: v for theDict in theDicts for k, v in theDict.items() }`!
katrielalex
@katrielalex: I **love** dict comprehension! But I hestitate to include them in my answers, as they are python 3 only... but I'll include it.
delnan
But we all use Py3k these days, right? =p
katrielalex
@katrielalex: An alarming number of people do not, sadly.
delnan
I know... those poor, deprived souls who don't have dict comprehensions...
katrielalex
dict comprehensions are in 2.7 too
gnibbler
A: 

This handles all the subdicts in your list, even if they have multiple items:

dict(sum((d.items() for d in L), []))

But I think update() is preferred:

>>> dd = {}
>>> for d in L: dd.update(d)
Paul McGuire
Downvote, quoi?
Paul McGuire
A: 
>>> dictlist = [{'a':1},{'b':2},{'c':1},{'d':2, 'e':3}]
>>> dict(kv for d in dictlist for kv in d.iteritems())
{'a': 1, 'c': 1, 'b': 2, 'e': 3, 'd': 2}
>>>

Note I added a second key/value pair to the last dictionary to show it works with multiple entries. Also keys from dicts later in the list will overwrite the same key from an earlier dict.

Dave Kirby