0
if instanceType == instance_type and operatingSystem == operating_system and tenancy == tenancy_db:
    sku_list.append(key)

In this if statement these variables, instanceType, operatingSystem and tenancy, are user input, how to handle not to check if any user input is None.

e.g. if instanceType is None, i would like to check

if operatingSystem == operating_system and tenancy == tenancy_db:
    sku_list.append(key)

e.g. if operatingSystem is None, i would like to check

if instanceType == instance_type and tenancy == tenancy_db:
    sku_list.append(key)

e.g. if both tenancy and instanceType is None, I would like to check:

if operatingSystem == operating_system:
    sku_list.append(key)

Simliarly, it depends on whether user input in None or something else, is there any other way to do this, or i have to implement nested if else?

2 Answers 2

2

One way is to declare a helper function:

equal_or_none = lambda x, y: x is None or x == y
if (
        equal_or_none(instanceType, instance_type) 
        and equal_or_none(operatingSystem, operating_system)
        and equal_or_none(tenancy, tenancy_db)):
    sku_list.append(key)
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use the or operator which will treat None as false:

if (instanceType or instance_type) == instance_type and \
   (operatingSystem or operating_system) == operating_system and \
   (tenancy or tenancy_db) == tenancy_db:
    sku_list.append(key)

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.