0

assume I have dictionary like this

d = {
    'key_1': 2,
    'key_2': 2,
    'key_3': 2,
    'key_4': 5,
}

I want to create a dictionary telling me that how many times each value was repeated:

result = {
    2: 3,
    5: 1,
)

my solution was

from collection import defaultdict

r = defaultdict(lambda: 0)

for value in d.values():
    r[value] +=1

my question is, Is there a simpler cleaner soltion for this question (instead of 3 lines of code can I have a one liner solution?)

aliva
  • 5,450
  • 1
  • 33
  • 44

0 Answers0