How can I write the line below in functional python using e.g. toolz?
dct1 = {1: 1, 2: 2}
dct2 = {2:2}
dct3 = {2:2, 3:3}
common_keys = set(dct1.keys()) & set(dct2.keys()) & set(dct3.keys())
If you want to try to write this in a functional style:
from functools import reduce
dct1 = {1: 1, 2: 2}
dct2 = {2: 2}
dct3 = {2: 2, 3: 3}
shared_keys = reduce(set.intersection, map(set, map(dict.keys, [dct1, dct2, dct3])))
First we create a list of the dictionaries.
Then we map
the dict.keys
function to each of them.
Then we map them to set
giving us sets of keys for each dictionary.
Finally, we reduce
those sets with the set.intersection
function.