Python | Check for None values in given dictionary
Last Updated :
27 Apr, 2023
Many times, while working with dictionaries, we wish to check for a non-null dictionary, i.e check for None values in given dictionary. This finds application in Machine Learning in which we have to feed data with no none values. Let's discuss certain ways in which this task can be performed.
Method #1 : Using all() + not operator + values()
The combination of above functions can be used to perform this particular task. In this, we check for all the values using all function extracted using values function. The not operator is used to inverse the result to check for any of None value.
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
# Using all() + not operator + values()
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Using all() + not operator + values()
# Check for Non None Dictionary values
res = not all(test_dict.values())
# printing result
print("Does Dictionary contain None value ? " + str(res))
Output : The original dictionary is : {'Gfg': 1, 'CS': None, 'for': 2}
Does Dictionary contain None value ? True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #2 : Using in operator + values() This task can also be performed using the in operator and values function. We just check for None in all the values extracted using the values function and check for existence using the in operator.
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
# Using in operator + values()
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Using in operator + values()
# Check for Non None Dictionary values
res = None in test_dict.values()
# printing result
print("Does Dictionary contain None value ? " + str(res))
Output : The original dictionary is : {'Gfg': 1, 'CS': None, 'for': 2}
Does Dictionary contain None value ? True
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1), constant extra space needed
Method #3 : Using values() and count() methods
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res=False
x=list(test_dict.values())
if(x.count(None)>=1):
res=True
# printing result
print("Does Dictionary contain None value ? " + str(res))
OutputThe original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True
Time complexity: O(n) where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n) as well because we are creating a new list of values that is the same length as the dictionary.
Method #4: Using filter()+list()+ lambda functions
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x = list(test_dict.values())
res = len(list(filter(lambda x: x == None, x))) > 0
# printing result
print("Does Dictionary contain None value ? " + str(res))
OutputThe original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5 : Using any()
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Using any()
# Check for Non None Dictionary values
res = any(val == None for val in test_dict.values())
# printing result
print("Does Dictionary contain None value ? " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 6: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of
# Check for Non None Dictionary values
import operator as op
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = op.countOf(test_dict.values(), None) > 0
# printing result
print("Does Dictionary contain None value ? " + str(res))
OutputThe original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 7: Using list comprehension
Python3
# Initialize a dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
# Print the original dictionary
print("The original dictionary is : " + str(test_dict))
# Use a list comprehension to find values that are None
res = [val for val in test_dict.values() if val is None]
# Check if the list is not empty
if res:
# If the list is not empty, it means the dictionary contains None values
print("Does Dictionary contain None value ? True")
else:
# If the list is empty, it means the dictionary does not contain None values
print("Does Dictionary contain None value ? False")
#This code is contributed by Vinay Pinjala.
OutputThe original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True
Time Complexity:O(N)
Auxiliary Space :O(N)
Method#8: Using Recursive method.
Algorithm contains_none_value(dictionary):
1. If dictionary is empty, return False
2. If None is in dictionary values, return True
3. For each value in dictionary values:
a. If the value is a dictionary, call contains_none_value recursively on the value
b. If the recursive call returns True, return True
4. None value not found in dictionary or nested dictionary, return False
Python3
def contains_none_value(dictionary):
# Base case: dictionary is empty
if not dictionary:
return False
# Check if any value in the dictionary is None
if None in dictionary.values():
return True
# Recursive case: check if any nested dictionary contains a None value
for value in dictionary.values():
if isinstance(value, dict):
if contains_none_value(value):
return True
# None value not found in dictionary or nested dictionary
return False
test_dict = {'Gfg': 1, 'for': 2, 'CS': None}
res=contains_none_value(test_dict)
print("Does Dictionary contain None value ?",res)
OutputDoes Dictionary contain None value ? True
Time complexity: O(n), where n is the total number of values in the dictionary and its nested dictionaries. This is because the method needs to iterate over all the values in the dictionary and its nested dictionaries. In the worst case, every value in the dictionary is a nested dictionary with no None value, so the method needs to visit every value in the dictionary and its nested dictionaries.
Auxiliary Space: O(d), where d is the maximum depth of the nested dictionaries. This is because the method uses recursion to traverse the nested dictionaries, and each recursive call adds a new frame to the call stack. In the worst case, the nested dictionaries are deeply nested, so the call stack grows to the depth of the nested dictionaries. However, in most cases, the nested dictionaries are not deeply nested, so the space complexity is much lower.
Similar Reads
Python | Check if key has Non-None value in dictionary 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 th
6 min read
Python - Check for Key in Dictionary Value list Sometimes, while working with data, we might have a problem we receive a dictionary whose whole key has list of dictionaries as value. In this scenario, we might need to find if a particular key exists in that. Let's discuss certain ways in which this task can be performed. Method #1: Using any() Th
6 min read
Check if Value Exists in Python Dictionary We are given a dictionary and our task is to check whether a specific value exists in it or not. For example, if we have d = {'a': 1, 'b': 2, 'c': 3} and we want to check if the value 2 exists, the output should be True. Let's explore different ways to perform this check in Python.Naive ApproachThis
2 min read
Python - Check if all values are K in dictionary While working with dictionary, we might come to a problem in which we require to ensure that all the values are K in dictionary. This kind of problem can occur while checking the status of the start or checking for a bug/action that could have occurred. Letâs discuss certain ways in which this task
9 min read
Python - Check for None value in Matrix Python supports a list as its list element and hence a matrix can be formed. Sometimes we might have a utility in which we require to perform None check in that list of list i.e matrix and its a very common in all the domains of coding, especially Data Science. Letâs discuss certain ways in which th
5 min read
Python | Check if all values are 0 in dictionary While working with dictionary, we might come to a problem in which we require to ensure that all the values are 0 in dictionary. This kind of problem can occur while checking status of start or checking for a bug/action that could have occurred. Let's discuss certain ways in which this task can be p
7 min read