hjelpmig
hjelpmig

Reputation: 1455

check if dictionary key has empty value

I have the following dictionary

dict1 ={"city":"","name":"yass","region":"","zipcode":"",
       "phone":"","address":"","tehsil":"", "planet":"mars"}

I am trying to create a new dictionary that will be based on dict1 but,

  1. it will not contain keys with empty strings.
  2. it will not contain those keys that I dont want to include.

i have been able to fulfill the requirement 2 but getting problem with requirement 1. Here is what my code looks like.

dict1 ={"city":"","name":"yass","region":"","zipcode":"",
   "phone":"","address":"","tehsil":"", "planet":"mars"}

blacklist = set(("planet","tehsil"))    
new = {k:dict1[k] for k in dict1 if k not in blacklist} 

this gives me the dictionary without the keys: "tehsil", "planet" I have also tried the following but it didnt worked.

new = {k:dict1[k] for k in dict1 if k not in blacklist and dict1[k] is not None}

the resulting dict should look like the one below:

new = {"name":"yass"}

Upvotes: 6

Views: 18509

Answers (6)

N V M
N V M

Reputation: 3

dict2 = { (k,v) for k,v in dict1.items() if ( k in whitelist ) and ( v != "" ) }

Upvotes: 0

njzk2
njzk2

Reputation: 39406

Testing if the value is not an empty string is not done using is not None.

An empty string evaluates as False, while any non-empty string evaluates as True. Hence, you can test it directly, and simply remove the is not None from your expression :

new = {k:dict1[k] for k in dict1 if k not in blacklist and dict1[k]}

Upvotes: 1

HennyH
HennyH

Reputation: 7944

This is a white list version:

>>> dict1 ={"city":"","name":"yass","region":"","zipcode":"",
       "phone":"","address":"","tehsil":"", "planet":"mars"}
>>> whitelist = ["city","name","planet"]
>>> dict2 = dict( (k,v) for k, v in dict1.items() if v and k in whitelist )
>>> dict2
{'planet': 'mars', 'name': 'yass'}

Blacklist version:

>>> blacklist = set(("planet","tehsil"))
>>> dict2 = dict( (k,v) for k, v in dict1.items() if v and k not in blacklist )
>>> dict2
{'name': 'yass'}

Both are essentially the same expect one has not in the other in. If you version of python supports it you can do:

>>> dict2 = {k: v for k, v in dict1.items() if v and k in whitelist}

and

>>> dict2 = {k: v for k, v in dict1.items() if v and k not in blacklist}

Upvotes: 5

jamylak
jamylak

Reputation: 133554

This would have to be the fastest way to do it (using set difference):

>>> dict1 = {"city":"","name":"yass","region":"","zipcode":"",
       "phone":"","address":"","tehsil":"", "planet":"mars"}
>>> blacklist = {"planet","tehsil"}
>>> {k: dict1[k] for k in dict1.viewkeys() - blacklist if dict1[k]}
{'name': 'yass'}

White list version (using set intersection):

>>> whitelist = {'city', 'name', 'region', 'zipcode', 'phone', 'address'}
>>> {k: dict1[k] for k in dict1.viewkeys() & whitelist if dict1[k]}
{'name': 'yass'}

Upvotes: 4

Ishpeck
Ishpeck

Reputation: 2041

You're on the right track. Consider:

Python 2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dict1 ={"city":"","name":"yass","region":"","zipcode":"",
...    "phone":"","address":"","tehsil":"", "planet":"mars"}
>>> 
>>> def isgood(undesired, key, val): return key not in undesired and key and val
... 
>>> dict([x for x in dict1.items() if isgood(["planet", "tehsil"], *x)])
{'name': 'yass'}

Upvotes: 1

Velimir Mlaker
Velimir Mlaker

Reputation: 10955

Simply test dict1[k] for Truth value (instead of is None.).

new = {k:dict1[k] for k in dict1 if k not in blacklist and dict1[k]}

Upvotes: 1

Related Questions