-2

Given a dictionary and an int value p, I want to return the sum of its values where the key is greater than p.

For example:

{5:3,10:5,15:7,20:9}, when p = 18 the result is 9

{20:3,40:5,60:7,80:9}, when p = 25 the result is 5 + 7 + 9 = 21

{10:1,20:2,30:3,40:4}, when p = 29 the result is 3 + 4 =7

What should I do to identify the integer key and sum up the value keys and return an answer?

CDJB
  • 14,043
  • 5
  • 29
  • 55
sillyrabbit
  • 11
  • 1
  • 2

1 Answers1

1

What about:

>>> d = {5:3,10:5,15:7,20:9}
>>> sum(v for k, v in d.items() if k > 18)
9
>>> d = {20:3,40:5,60:7,80:9}
>>> sum(v for k, v in d.items() if k > 25)
21
>>> d = {10:1,20:2,30:3,40:4}
>>> sum(v for k, v in d.items() if k > 29)
7
CDJB
  • 14,043
  • 5
  • 29
  • 55