Open In App

Python – Filter Non-None dictionary Keys

Last Updated : 11 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Non-None dictionary Keys
# Using loop
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using loop
# Non-None dictionary Keys
res = []
for ele in test_dict:
    if test_dict[ele] is not None :
        res.append(ele)
 
# printing result
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




# Python3 code to demonstrate working of
# Non-None dictionary Keys
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Non-None dictionary Keys
# Using dictionary comprehension
res = list({ele for ele in test_dict if test_dict[ele]})
 
# printing result
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




# Python3 code to demonstrate working of
# Non-None dictionary Keys
# Using filter() and lambda function
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Non-None dictionary Keys
# Using filter() and lambda function
res = list(filter(lambda x: test_dict[x] is not None, test_dict))
 
# printing result
print("Non-None keys list : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using list comprehension
res = [key for key, value in test_dict.items() if value is not None]
 
# printing result
print("Non-None keys list : " + str(res))
#This code is contributed by Vinay Pinjala.


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




# Python3 code to demonstrate working of
# Non-None dictionary Keys
# Using map() and filter()
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : None}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using map() and filter()
# Non-None dictionary Keys
res = list(filter(lambda x: x is not None, map(lambda x: x if test_dict[x] is not None else None, test_dict)))
 
# printing result
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:

  1. Define a recursive function non_none_keys that takes a dictionary as input.
  2. Check if the dictionary is empty. If so, return an empty list.
  3. If the dictionary is not empty, pop an item from the dictionary.
  4. Recursively call non_none_keys on the remaining dictionary and store the result in a variable res
  5. Check if the value of the popped item is not None. If so, append the key to res.
  6. Return res.
  7. Initialize a dictionary test_dict.
  8. Print the original dictionary.
  9. Call non_none_keys on a copy of the dictionary and store the result in res.
  10. 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))
#This code is contributed by Jyothi Pinjala


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
Previous
Next
Share your thoughts in the comments

Similar Reads