Home > OS >  Get list of keys from a dictionary matches to list of values
Get list of keys from a dictionary matches to list of values

Time:12-02

dictionary = {"raj": 2, "striver": 3, "vikram": 4}
myvals = [2,4]

Need to get ['raj', 'vikram'] as a list filtering above dictionary using myvals list

tried

result = [dictionary[k] for k in [2, 4]]
result = [dictionary[k] for k in myvals]

CodePudding user response:

[k for k, v in dictionary.items() if v in myvals]
  • Related