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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With