18

Let's say I have a dictionary:

dict1 = {"Jim": "y", "Bob": "y", "Ravioli": "n"}  # etc.

I want to print out all the keys with the value "y" (i.e: "Jim", "Bob"). How do I achieve this (in the simplest way possible)?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Cobie Fisher
  • 713
  • 2
  • 8
  • 18

1 Answers1

37

You can use this:

[k for k, v in dict1.items() if v == 'y']

Result:

['Bob', 'Jim']
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Rahul K P
  • 15,740
  • 4
  • 35
  • 52