Hello guys I'm trying to iterate from a dictionary to get the keys in case some of keys would be empty, but I have no idea how to achieve this.
Any idea ?
def val(**args):
args = args
print args
# print args
# print (args.keys())
val(name = '', country = 'Canada', phone = '')
Whit this example I got {'country': 'Canada', 'name': '', 'phone': ''} but when I'm really looking is to get only the keys of the empty keys in a list using append, the problem is that it gives me all the keys when and not just the empty keys.
In that case I would like to return something like this: name, phone
I appreciate your help.
Iterate the dictionary and extract keys where the value is an empty string:
empty_keys = [k for k, v in args.items() if v == '']
or as a function:
>>> def val(**args):
... return [k for k, v in args.items() if v == '']
...
>>> val(name = '', country = 'Canada', phone = '')
['phone', 'name']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With