0

I have a piece of code that needs to run if any of the variables match some strings. The code seems long and not very pythonic:

if candidate_job_title in ('ERROR', 'NOT AVAILABLE') or candidate_company in ('ERROR', 'NOT AVAILABLE') or candidate_location in ('ERROR', 'NOT AVAILABLE'):  
# do something

the best I could come up with is this, is there anything else I can do to make it more readable?

if any (v in ('ERROR', 'NOT AVAILABLE') for v in (candidate_job_title, candidate_company, candidate_location):
# do something
6
  • In your first code sample, you mentioned candidate_company twice. I think one of them should be candidate_location. Commented Mar 26, 2021 at 10:42
  • Do all candidate properties need to be checked against the same set of strings? Commented Mar 26, 2021 at 10:50
  • no, only a subset of all candidate properties need to be checked. Commented Mar 26, 2021 at 10:52
  • No, I meant do you always need to check against ('ERROR', 'NOT AVAILABLE') only? Or can there be any other string for location and something else for company? Commented Mar 26, 2021 at 10:53
  • ok, got you now. No, always the same set of strings. Commented Mar 26, 2021 at 10:54

1 Answer 1

5

If these values are hashable you can use set intersection:

if {candidate_job_title, candidate_company, candidate_location} & {'ERROR', 'NOT AVAILABLE'}:
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.