'find average and sum of unique values count for dictionary values

I have an input dictionary like d={'a':['ar','br','cb','dr'],'bn':['ar','bm','cm','dm']}, there is a need to find out average for values count within the dictionary. Also, I want to find out all the unique values within the dictionary. I need a generalized solution. The output I expect is for average the result should be 4. For the sum of all unique values count, I expect 7 as output.

d={'a':['ar','br','cb','dr'],'bn':['ar','bm','cm','dm']} Output: for average output should be 4. for unique values count output should be 7.



Solution 1:[1]

For the mean:

sum(len(v) for v in d.values())/len(d)

For the uniques:

len( set(item for v in d.values() for item in v))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Learning is a mess