Finding all the keys with the same value in a Python dictionary

Question:

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

Asked By: Cobie Fisher

||

Answers:

You can use this:

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

Result:

['Bob', 'Jim']
Answered By: Rahul K P
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.