Skip to content
Advertisement

python: union keys from multiple dictionary?

I have 5 dictionaries and I want a union of their keys.

JavaScript

I tried

JavaScript

but it gave me an error

JavaScript

Am I doing it wrong ? I using normal forloop but I wonder why the above code didn’t work.

Advertisement

Answer

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:

JavaScript

An alternative without any lambdas would be:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement