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
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
res = False
if test_dict[ 'gfg' ]:
res = True
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
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
res = bool (test_dict.get( 'gfg' ))
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:
- 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.
- Print the original dictionary using the print() function.
- Using a ternary operator, check if the key ‘gfg’ is present in the dictionary test_dict and if its value is not None.
- a. If the condition is True, set the value of res to True.
- b. If the condition is False, set the value of res to False.
- Print the result of the check using the print() function.
Python3
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
res = True if 'gfg' in test_dict and test_dict[ 'gfg' ] is not None else False
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:
- Initialize the dictionary with key-value pairs.
- Print the original dictionary.
- Use the “try-except” block to check if the ‘gfg’ key has a non-None value.
- If the key is present and has a non-None value, set the res variable to True, else set it to False.
- If the key is not present in the dictionary, set the res variable to False.
- Print the result.
Python3
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
try :
if test_dict[ 'gfg' ] is not None :
res = True
else :
res = False
except KeyError:
res = False
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:
- Initialize the dictionary.
- Print the original dictionary.
- Use the dictionary’s get() method with a default value of None to check if the key has a non-None value.
- If the value returned by get() is not None, set the result to True, else False.
- Print the result.
Python3
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
res = True if test_dict.get( 'gfg' , None ) is not None else False
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:
- Initialize the dictionary.
- Print the original dictionary.
- Check if a key has a Non-None value in the dictionary using the “in” keyword.
- Store the result in a boolean variable.
- Print the result.
Example:
Python3
test_dict = { 'gfg' : None , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
res = 'gfg' in test_dict and test_dict[ 'gfg' ] is not None
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Apr, 2023
Like Article
Save Article