Open In App

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

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python dictionaries, we might come across a problem in which we need to find if a particular key of the dictionary is valid i.e it is not False or has a non-none value. This kind of problem can occur in the 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

Time complexity:

The get() method has an average time complexity of O(1) and a worst-case time complexity of O(n) where n is the size of the dictionary.
The bool() function has a constant time complexity of O(1).
Therefore, the time complexity of the code is O(1).

Auxiliary space complexity:

The code uses constant auxiliary space to store the dictionary and a few variables.
Therefore, the auxiliary space complexity of the code is O(1).

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

Time complexity: O(1) since dictionary lookups are constant time in python. 
Auxiliary Space: O(1) since we only use constant additional memory.

Method 4: using the “try-except” block to check if the key has a non-None value in the dictionary:

Approach:

  1. Initialize the dictionary with key-value pairs.
  2. Print the original dictionary.
  3. Use the “try-except” block to check if the ‘gfg’ key has a non-None value.
  4. If the key is present and has a non-None value, set the res variable to True, else set it to False.
  5. If the key is not present in the dictionary, set the res variable to False.
  6. Print the result.

Python3




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

Time complexity: O(1) since dictionary lookup is a constant time operation.
Auxiliary space: O(1) since the space used by the variables is constant irrespective of the size of the dictionary.

Method #5: Using the dictionary’s get() method with a default value

Here’s the step-by-step approach:

  1. Initialize the dictionary.
  2. Print the original dictionary.
  3. Use the dictionary’s get() method with a default value of None to check if the key has a non-None value.
  4. If the value returned by get() is not None, set the result to True, else False.
  5. Print the result.

Python3




# Python3 code to demonstrate working of
# Check if key has Non-None value in dictionary
# Using dictionary's get() method with default value
 
# Initialize dictionary
test_dict = {'gfg' : None, 'is' : 4, 'for' : 2, 'CS' : 10}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using dictionary's get() method with default value
# Check if key has Non-None value in dictionary
res = True if test_dict.get('gfg', None) 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

Time complexity: O(1)
Auxiliary space: O(1)

Method #6: Using the “in” keyword with a direct comparison to None value.

Approach:

  1. Initialize the dictionary.
  2. Print the original dictionary.
  3. Check if a key has a Non-None value in the dictionary using the “in” keyword.
  4. Store the result in a boolean variable.
  5. Print the result.

Example: 

Python3




# Initialize dictionary
test_dict = {'gfg': None, 'is': 4, 'for': 2, 'CS': 10}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using the "in" keyword with a direct comparison to None value
# Check if key has Non-None value in dictionary
res = 'gfg' in test_dict and test_dict['gfg'] is not None
 
# 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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads