Python - Remove empty value types in dictionaries list - GeeksforGeeks (2024)

Last Updated : 25 Apr, 2023

Summarize

Comments

Improve

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))

Output :

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))

Output :

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:

  1. Create an empty list res to hold the filtered dictionaries.
  2. Traverse each dictionary d in the input list test_list.
  3. For each dictionary d, create a new dictionary with key-value pairs where the value is not None. Add the new dictionary to res.
  4. 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))

Output

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:

  1. Create a list comprehension to iterate over each dictionary in the original list (test_list).
  2. For each dictionary (sub), create a dictionary comprehension to iterate over its key-value pairs (items).
  3. Use an if statement to check if the value is truthy (i.e. not None, empty string, empty list, etc.).
  4. If the value is truthy, include the key-value pair in the new dictionary being created.
  5. 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.

Output

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))

Output

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:

  1. Import the json library
  2. Convert the list of dictionaries to a JSON string using the dumps() function
  3. Load the JSON string back to a list of dictionaries using the loads() function
  4. Use list comprehension to filter the dictionaries based on the condition that all values in the dictionary are not None or empty
  5. 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))

Output

The filtered list : [{'gfg': 4}, {'like': 5}]

Time complexity: O(n)
Auxiliary space: O(n) (for storing the JSON string)



manjeet_04

Python - Remove empty value types in dictionaries list - GeeksforGeeks (2)

Improve

Next Article

Python | Remove empty tuples from a list

Please Login to comment...

Python - Remove empty value types in dictionaries list - GeeksforGeeks (2024)
Top Articles
How to Research Gold Spot Prices
Convert $25 per hour to Biweekly salary | Talent.com
Roblox Roguelike
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Locate Td Bank Near Me
Seth Juszkiewicz Obituary
Where's The Nearest Wendy's
今月のSpotify Japanese Hip Hopベスト作品 -2024/08-|K.EG
Med First James City
Summoners War Update Notes
Trini Sandwich Crossword Clue
Flights To Frankfort Kentucky
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
What is Rumba and How to Dance the Rumba Basic — Duet Dance Studio Chicago | Ballroom Dance in Chicago
Air Force Chief Results
Accident On The 210 Freeway Today
Robeson County Mugshots 2022
Sussyclassroom
Jail View Sumter
Brbl Barber Shop
Foodsmart Jonesboro Ar Weekly Ad
Gillette Craigslist
897 W Valley Blvd
Maths Open Ref
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
N.J. Hogenkamp Sons Funeral Home | Saint Henry, Ohio
Isablove
APUSH Unit 6 Practice DBQ Prompt Answers & Feedback | AP US History Class Notes | Fiveable
Hotel Denizen Mckinney
Solve 100000div3= | Microsoft Math Solver
Tamilrockers Movies 2023 Download
Upstate Ny Craigslist Pets
Texters Wish You Were Here
Old Peterbilt For Sale Craigslist
Lake Dunson Robertson Funeral Home Lagrange Georgia Obituary
Despacito Justin Bieber Lyrics
Devin Mansen Obituary
New Gold Lee
Doordash Promo Code Generator
Panorama Charter Portal
Achieving and Maintaining 10% Body Fat
Traumasoft Butler
Winta Zesu Net Worth
What to Do at The 2024 Charlotte International Arts Festival | Queen City Nerve
Crystal Glassware Ebay
Take Me To The Closest Ups
Enter The Gungeon Gunther
6463896344
Ocean County Mugshots
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
La Fitness Oxford Valley Class Schedule
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6571

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.