Python: union keys from multiple dictionary?

Your solution works for the first two elements in the list, but then dict1 and dict2 got reduced into a set and that set is put into your lambda as the x So now x does not have the method keys() anymore The solution is to make x be a set from the very beginning by initializing the reduction with an empty set (which happens to be the neutral element of the union) Try it with an initializer: allkey = reduce(lambda x, y: x. Union(y.keys()), alldict, set()) An alternative without any lambdas would be: allkey = reduce(set. Union, map(set, map(dict.

Keys, alldict))).

Your solution works for the first two elements in the list, but then dict1 and dict2 got reduced into a set and that set is put into your lambda as the x. So now x does not have the method keys() anymore. The solution is to make x be a set from the very beginning by initializing the reduction with an empty set (which happens to be the neutral element of the union).

Try it with an initializer: allkey = reduce(lambda x, y: x. Union(y.keys()), alldict, set()) An alternative without any lambdas would be: allkey = reduce(set. Union, map(set, map(dict.

Keys, alldict))).

Almost... try set() as the initializer – itsadok Mar 9 at 6:52.

I think @chuck already answered the question why it doesn't work, but a simpler way to do this would be to remember that the union method can take multiple arguments: allkey = set(). Union(*alldict) does what you want without any loops or lambdas.

Good call. Have an upvote, Sir! – chuck Mar 9 at 10:06.

A simple strategy for non-functional neurons (pun intended): allkey = for dictio in alldict: for key in dictio: allkey. Append(key) allkey = set(allkey).

If this were a list comprehension, i'd upvote it. – Björn Pollex Mar 9 at 8:01.

Just one more way, 'cause what the hay: a={}; a. Update(b) for be in alldict and a.keys() or the slightly-more-mysterious reduce(lambda a, b: a. Update(b) or a, alldict, {}).keys() (I'm bummed that there's no built-in function equivalent to def f(a,b): r = {} r.

Update(a) r. Update(b) return r is there? ).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send