Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the keys using the values with some conditions in a dictionary using python?

I have a dictionary like this:

 {'1956': 21,
     '2188': 76,
     '1307': 9,
     '1305': 22,
     '2196': 64,
     '3161': 1,
     '1025': 22,
     '321': 60,
     '1959': 1,
     '1342': 7,
     '3264': 2}

Where every keys are unique, I want to fetch those keys from the dictionary whose values are more than 60.

The output look like:

  ['2188','2196']

I can do it using a loop iteration using get('key') with a if condition, But that is a long process, what is the shortcut to do it more efficiently ?

like image 230
Kallol Avatar asked May 11 '26 21:05

Kallol


1 Answers

[k for k, v in mydict.items() if v > 60]
like image 73
ipaleka Avatar answered May 14 '26 11:05

ipaleka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!