Jwalin Shah
Jwalin Shah

Reputation: 2521

How to check condition for key and value in python dictionary for specific value

how to check whether my key has value of <class 'inspect._empty'>

Dictionary looks like :

{'abc': <class 'inspect._empty'>, 'xyz': None}

Now, I want to set abc equal to x value if it has value <class 'inspect._empty'>

    def _arg_unset(args_dict: Dict, key: str) -> bool:
    """return True if key is not set to some actual value in args_dict"""
    return args_dict.get(key) in {None, Parameter.empty}

In above cod, dict needs to be passed as first param and key as second param.

Upvotes: 1

Views: 64

Answers (1)

Sunitha
Sunitha

Reputation: 12005

>>> import inspect
>>> d = {'abc': inspect._empty, 'xyz': None}
>>> d
{'abc': <class 'inspect._empty'>, 'xyz': None}
>>> {k:v if v != inspect._empty else 'x' for k,v in d.items()}
{'abc': 'x', 'xyz': None}
>>> 

Upvotes: 2

Related Questions