Python | Check for None values in given dictionary
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 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)) |
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 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)) |
The original dictionary is : {'Gfg': 1, 'CS': None, 'for': 2} Does Dictionary contain None value ? True
Recommended Posts:
- Python | Check if all values are 0 in dictionary
- Python dictionary | values()
- Python | N largest values in dictionary
- Python | Sum values for each key in nested dictionary
- Python | Selective key values in dictionary
- Python | Initialize dictionary with None values
- Python | Max/Min of tuple dictionary values
- Ways to extract all dictionary values | Python
- Python | Combining values from dictionary of list
- Python | Type conversion in dictionary values
- Python | Get Unique values from list of dictionary
- Python | Summation of dictionary list values
- Map function and Dictionary in Python to sum ASCII values
- Python | Create a dictionary using list with none values
- Python | Grouping list values into dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.