Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using OR in if statement to see if either query param present

I am looking to implement a simple filter using a single query param (eg age=gt:40, name=eq:bob). I am wondering if it is possible to check if either name or age is present in the GET request at once? An example might clarify what I’m after:

if ('age' or 'name') in request.GET:

This will only match when the first one is used. When I hit the endpoint with the query param name it doesn’t match true.

I know I could do something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if ('age' in request.GET) or ('name' in request.GET) :

but this could grow quite quickly and become ugly.

>Solution :

You can use any(…) [Python-doc]:

if any(x in request.GET for x in ('age', 'name')):
    # …
    pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading