Home > Blockchain >  Finding the length of keys of dictionary based on values
Finding the length of keys of dictionary based on values

Time:10-14

input_dict = {'ab':12, 'cd':4, 'ef':1, 'gh':8, 'kl':9}
out_dict = 2

Is there any way to find the length of keys of the dictionary, if the values in dictionary are greater than 2 and less than 9?

CodePudding user response:

I think you want to find number of items in dictionary where value is 2 < v < 9:

input_dict = {"ab": 12, "cd": 4, "ef": 1, "gh": 8, "kl": 9}

out = sum(2 < v < 9 for v in input_dict.values())
print(out)

Prints:

2

CodePudding user response:

Try this,

In [61]: len([v for v in d.values() if 2 < v < 9])
Out[61]: 2
  • Related