0

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())
Chris
  • 26,361
  • 5
  • 21
  • 42
gebbissimo
  • 2,137
  • 2
  • 25
  • 35

3 Answers3

2

Actualy your question is very unclear. I will try to help you with:

First of all, you don't need to use set over dict.keys():

dct1.keys() & dct2.keys() & dct3.keys()
# returns {2}

You can use reduce function for your puprose:

from functools import reduce    


def key_intersection(*dicts):
    if len(dicts) == 0:
        return set([])
    if len(dicts) == 1:
        return set(dicts[0])
    return reduce(lambda l, r: l & r.keys(), dicts[1:], set(dicts[0]))


key_intersection(dct1, dct2, dct3)
eightlay
  • 423
  • 2
  • 9
2

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.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    This answer highlights how the methods of python data types are implicitly functions to be called on their instances and that by calling `cls.method(instance)` we something close to a pure function. – philosofool Jul 13 '22 at 02:08
1

Here is a clean way using toolz. Using toolz.pipe and toolz.curried often makes things easier to compose and easier to read:

import operator as op
from toolz import pipe
from toolz.curried import map, reduce

pipe(
    [dct1, dct2, dct3],
    map(op.methodcaller("keys")),
    reduce(op.and_)
)

I found your question clear, and I would say this is the best functional toolz-style way of doing this. This pattern is powerful and can be extended to more complicated problems, so it is worth learning.

eriknw
  • 286
  • 1
  • 2