Sometimes, while working with Python dictionaries, we require to remove all the values that are virtually Null, i.e does not hold any meaningful value and are to be removed before processing data, this can be an empty string, empty list, dictionary, or even 0. This has applications in data preprocessing. Let us discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension
This is shorthand to brute way by which this task can be performed. In this, we remake the dictionary with only the valid values.
Python3
# Python3 code to demonstrate working of # Remove None value types in dictionaries list # Using list comprehension # initializing list test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] # printing original list print ( "The original list is : " + str (test_list)) # Remove None value types in dictionaries list # Using list comprehension res = [ele for ele in ({key: val for key, val in sub.items() if val} for sub in test_list) if ele] # printing result print ( "The filtered list : " + str (res)) |
The original list is : [{'is': '', 'best': [], 'gfg': 4}, {'like': 5, 'gfg': 0, 'I': {}}] The filtered list : [{'gfg': 4}, {'like': 5}]
Time Complexity: O(n) where n is the number of elements in the dictionary. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #2: Using filter() + lambda + list comprehension
The combination of the above methods can be used to solve this problem. In this, we use filter() + lambda to perform the conditional statement task as in the above method to reduce a nesting level.
Python3
# Python3 code to demonstrate working of # Remove None value types in dictionaries list # Using filter() + lambda + list comprehension # initializing list test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] # printing original list print ( "The original list is : " + str (test_list)) # Remove None value types in dictionaries list # Using filter() + lambda + list comprehension res = list ( filter ( None , ({key: val for key, val in sub.items() if val} for sub in test_list))) # printing result print ( "The filtered list : " + str (res)) |
The original list is : [{'is': '', 'best': [], 'gfg': 4}, {'like': 5, 'gfg': 0, 'I': {}}] The filtered list : [{'gfg': 4}, {'like': 5}]
Method #3: Using a for loop.
Algorithm:
- Create an empty list res to hold the filtered dictionaries.
- Traverse each dictionary d in the input list test_list.
- For each dictionary d, create a new dictionary with key-value pairs where the value is not None. Add the new dictionary to res.
- Return the final filtered list res.
Python3
# initializing list test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] # printing original list print ( "The original list is : " + str (test_list)) # Remove None value types in dictionaries list # Using for loop res = [] for d in test_list: res.append({k: v for k, v in d.items() if v}) # printing result print ( "The filtered list : " + str (res)) |
The original list is : [{'gfg': 4, 'is': '', 'best': []}, {'I': {}, 'like': 5, 'gfg': 0}] The filtered list : [{'gfg': 4}, {'like': 5}]
Time Complexity: O(n*m), where n is the number of dictionaries in the list and m is the maximum number of key-value pairs in a dictionary.
Auxiliary Space: O(n*m), since we are creating a new dictionary for each dictionary in the input list.
Method#4:Using list comprehension and if statement
Approach:
- Create a list comprehension to iterate over each dictionary in the original list (test_list).
- For each dictionary (sub), create a dictionary comprehension to iterate over its key-value pairs (items).
- Use an if statement to check if the value is truthy (i.e. not None, empty string, empty list, etc.).
- If the value is truthy, include the key-value pair in the new dictionary being created.
- Append the new dictionary to the final list (res) if it contains at least one key-value pair.
Example:
Python3
test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] res = [{k: v for k, v in sub.items() if v} for sub in test_list if any (sub.values())] print ( "The filtered list : " + str (res)) # This code is contributed by Vinay Pinjala. |
The filtered list : [{'gfg': 4}, {'like': 5}]
Time complexity:
The time complexity of this code is O(n*m), where n is the number of dictionaries in the input list and m is the number of key-value pairs in each dictionary. The list comprehension iterates over each dictionary in the input list, and the dictionary comprehension iterates over each key-value pair in each dictionary.
Auxiliary Space:
The space complexity of this code is O(n*m), where n is the number of dictionaries in the input list and m is the maximum number of key-value pairs in any dictionary. The new list (res) contains a new dictionary for each dictionary in the input list that contains at least one truthy value, and each new dictionary contains a subset of the original key-value pairs.
Method #5: Using dictionary comprehension and if statement
using list comprehension, but instead of using a nested dictionary comprehension, we use a single dictionary comprehension inside the outer list comprehension. The if statement is used to filter out None values.
Python3
# Python3 code to demonstrate working of # Remove None value types in dictionaries list # Using dictionary comprehension # initializing list test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] # printing original list print ( "The original list is : " + str (test_list)) # Remove None value types in dictionaries list # Using dictionary comprehension and if statement res = [{key: val for key, val in sub.items() if val is not None } for sub in test_list] # printing result print ( "The filtered list : " + str (res)) |
The original list is : [{'gfg': 4, 'is': '', 'best': []}, {'I': {}, 'like': 5, 'gfg': 0}] The filtered list : [{'gfg': 4, 'is': '', 'best': []}, {'I': {}, 'like': 5, 'gfg': 0}]
Time Complexity: O(n*m), where n is the number of dictionaries in the list and m is the average number of key-value pairs in each dictionary.
Auxiliary Space: O(n*m), where n is the number of dictionaries in the list and m is the average number of key-value pairs in each dictionary.
Method 6: Using the json library
Approach:
- Import the json library
- Convert the list of dictionaries to a JSON string using the dumps() function
- Load the JSON string back to a list of dictionaries using the loads() function
- Use list comprehension to filter the dictionaries based on the condition that all values in the dictionary are not None or empty
- Print the filtered list.
Example:
Python3
# import json library import json # initializing list test_list = [{ 'gfg' : 4 , 'is' : ' ', ' best ': []}, {' I ': {}, ' like ': 5, ' gfg': 0 }] # convert list of dictionaries to JSON string json_str = json.dumps(test_list) # load JSON string back to list of dictionaries res = json.loads(json_str) # use list comprehension to filter the dictionaries res = [{k: v for k, v in d.items() if v} for d in res] # print the filtered list print ( "The filtered list : " + str (res)) |
The filtered list : [{'gfg': 4}, {'like': 5}]
Time complexity: O(n)
Auxiliary space: O(n) (for storing the JSON string)