Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Check for None values in given dictionary

Improve Article
Save Article
  • Last Updated : 26 Jan, 2023
Improve Article
Save Article

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

  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

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))


Output

The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True

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))


Output

The 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


Output

The 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))


Output

The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Does Dictionary contain None value ? True

Time Complexity: O(N)

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!