Python – Filter Non-None dictionary Keys
Last Updated :
11 May, 2023
Many times, while working with dictionaries, we wish to get keys for a non-null keys. 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 loop In this we just run a loop for all the keys and check for values, if its not None, we append into a list which stores keys of all Non None keys.
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = []
for ele in test_dict:
if test_dict[ele] is not None :
res.append(ele)
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['Gfg', 'for']
Time Complexity: O(n*n) where n is the number of elements in the string list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.
Method #2 : Using dictionary comprehension This task can also be performed using dictionary comprehension. In this, we perform similar operation as above method, just as a shorthand.
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = list ({ele for ele in test_dict if test_dict[ele]})
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['for', 'Gfg']
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using filter() and lambda function
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = list ( filter ( lambda x: test_dict[x] is not None , test_dict))
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['Gfg', 'for']
This approach is similar to the first one where we use a loop to iterate through the keys, but here we use the filter() function in combination with a lambda function to filter out the keys that have a non-None value.
Time complexity: O(n) – where n is the number of keys in the dictionary.
Auxiliary Space: O(n) – as we are creating a new list to store the non-None keys.
Method #4: using list comprehension
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = [key for key, value in test_dict.items() if value is not None ]
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['Gfg', 'for']
Time Complexity:O(N)
Auxiliary Space :O(N)
Method 5: Using the map() function with a lambda function.
Step-by-step approach:
- Initialize the dictionary.
- Use map() function with a lambda function to map each key to itself if its value is not None, or to None otherwise.
- Use filter() function with a lambda function to filter out the None values from the mapped list.
- Convert the filtered list to a list using list() function.
Below is the implementation of the above approach:
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = list ( filter ( lambda x: x is not None , map ( lambda x: x if test_dict[x] is not None else None , test_dict)))
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['Gfg', 'for']
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the filtered list.
Method 6: Using Recursion method:
- Define a recursive function non_none_keys that takes a dictionary as input.
- Check if the dictionary is empty. If so, return an empty list.
- If the dictionary is not empty, pop an item from the dictionary.
- Recursively call non_none_keys on the remaining dictionary and store the result in a variable res
- Check if the value of the popped item is not None. If so, append the key to res.
- Return res.
- Initialize a dictionary test_dict.
- Print the original dictionary.
- Call non_none_keys on a copy of the dictionary and store the result in res.
- Print the resulting list.
Python3
def non_none_keys(dictionary):
if not dictionary:
return []
key, value = dictionary.popitem()
res = non_none_keys(dictionary)
if value is not None :
res.append(key)
return res
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : None }
print ( "The original dictionary is : " + str (test_dict))
res = non_none_keys(test_dict.copy())
print ( "Non-None keys list : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': None}
Non-None keys list : ['Gfg', 'for']
Time complexity: O(n), where n is the number of items in the dictionary.
Space complexity: O(n), due to the recursive calls and the storage of the resulting list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...