Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 24 Mar, 2023
Improve Article
Save Article

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




# 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

Time Complexity: O(1)
Auxiliary Space: O(1)

  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




# 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

Method 3: using the in keyword and a ternary operator:

  1. Initialize a dictionary test_dict with key-value pairs. In this dictionary, the key ‘gfg’ has a None value, and the other keys have some integer values.
  2. Print the original dictionary using the print() function.
  3. Using a ternary operator, check if the key ‘gfg’ is present in the dictionary test_dict and if its value is not None.
  4. a. If the condition is True, set the value of res to True.
  5. b. If the condition is False, set the value of res to False.
  6. Print the result of the check using the print() function.

Python3




# Python3 code to demonstrate working of
# Check if key has Non-None value in dictionary
# Using ternary operator
 
# Initialize dictionary
test_dict = {'gfg' : None, 'is' : 4, 'for' : 2, 'CS' : 10}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using ternary operator
# Check if key has Non-None value in dictionary
res = True if 'gfg' in test_dict and test_dict['gfg'] is not None else False
 
# 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

.

The time complexity of this approach is O(1) since dictionary lookups are constant time in Python, 

Space complexity is O(1) since we only use constant additional memory.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!