Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Check if key has Non-None value in dictionary

  • Last Updated : 26 Jul, 2019

Sometimes, while working with Python dictionaries, we might come across a problem in which we need to find if a particular key of dictionary is valid i.e it is not False or has a non None value. This kind of problem can occur in Machine Learning domain. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using if
This task can simply be solved using the conditional operator "if". The if statement autochecks for the truthness of any statement and hence with the key’s value.




# Python3 code to demonstrate working of
# Check if key has Non-None value in dictionary
# Using if
  
# Initialize dictionary
test_dict = {'gfg' : None, 'is' : 4, 'for' : 2, 'CS' : 10}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Using if
# Check if key has Non-None value in dictionary
res = False
if test_dict['gfg']:
    res = True
      
# printing result 
print("Does gfg have a Non-None value? : " + str(res))
Output :

The original dictionary : {‘gfg’: None, ‘is’: 4, ‘for’: 2, ‘CS’: 10}
Does gfg have a Non-None value? : False

 

Method #2 : Using bool() + get()
The above functions together can be used to perform this particular task. The get performs the task of getting the value corresponding a key and bool function checks for truthfulness.




# Python3 code to demonstrate working of
# Check if key has Non-None value in dictionary
# Using bool() + get()
  
# Initialize dictionary
test_dict = {'gfg' : None, 'is' : 4, 'for' : 2, 'CS' : 10}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Using bool() + get()
# Check if key has Non-None value in dictionary
res = bool(test_dict.get('gfg'))
      
# printing result 
print("Does gfg have a Non-None value? : " + str(res))
Output :

The original dictionary : {‘gfg’: None, ‘is’: 4, ‘for’: 2, ‘CS’: 10}
Does gfg have a Non-None value? : False


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!