Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Iterating in a dictionary to get empty key's

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.

like image 428
El arquitecto Avatar asked Jan 16 '26 21:01

El arquitecto


1 Answers

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']
like image 192
Uriel Avatar answered Jan 19 '26 19:01

Uriel