Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding All The Keys With the Same Value in a Python Dictionary [duplicate]

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)?

like image 600
Cobie Fisher Avatar asked Feb 24 '17 12:02

Cobie Fisher


1 Answers

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.

like image 200
Rahul K P Avatar answered Sep 20 '22 10:09

Rahul K P