You need to iterate over the dictionary, decreasing it with an initial value of zero.
Note dictionary iteration, actually key iteration, so you need to index the dictionary to get the value
reduce(lambda x, key:x + h[key] * (h[key] - 1), h, 0)
Alternatively, since you are only interested in the dictionary values ββthat care the least for the key, just iterate over the dictionary values
Python 2.X
reduce(lambda x, value:x + value * (value - 1), h.itervalues(), 0)
Python 3.X
reduce(lambda x, value:x + value * (value - 1), h.values(), 0)
source share