I want to do this:
d = {1: 2, 3: 4}
if 4 in d:
print('Aha!')
but I want to read from the values and not the keys. What’s the Pythonic way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
values1:Note that this will be much slower than a key lookup because it will potentially require inspecting all values in the dictionary. If you need to perform this operation often you might want to consider storing the values in a
set.1
itervaluesin Python 2