12

Let's say I have a dictionary:

dict = {"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)?

Cobie Fisher
  • 635
  • 2
  • 8
  • 17

1 Answers1

30

Try this,

In [26]: [k for k,v in dict1.items() if v == 'y']
Out[26]: ['Bob', 'Jim']

And please don't use dict as a variable name.

Rahul K P
  • 10,793
  • 3
  • 32
  • 47