[python]Checking if all keys in dictionary exist

There is a chance when you need to verify all params in the dictionary are present before submitting a post request to an API server.

This is one of the solution which I am using:

from typing import Dict, Tuple, Union, List


def missing_params(test_object: Dict = None, ref: Tuple = None) -> Union[List, None]:
    missing = [k for k in ref if k not in test_object]
    return missing if missing else None

if __name__ == "__main__":
    payload = dict(
        username="username",
        password="password",
        address="address",
        extra_vars="extra_vars"
    )

    required = "username", "password", "address", "special_vars", "index_vars"

    missing = missing_params(test_object=payload, ref=required)
    if missing is None:
        print("All params exist.")
    else:
        print(f"The missing params is/are: {', '.join(missing)}")

The use of all function evaluates an iterable and returns true if all elements are true.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s