Python Forum
Remove empty keys in a python list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove empty keys in a python list
#1
I have a list of dictionaries and in case if some dictionary has empty key I want to remove it completely from the list. In my example,
I need to remove all third block because column3 is empty.

a = [
    {
        "type": "some.type2",
        "payload": {
            "column1": "12345",
            "column2": "description",
            "column3": 98
        }
    },
    {
        "type": "some.type2",
        "payload": {
            "column1": "12345",
            "column2": "description",
            "column3": 180
        }
    },
    {
        "type": "some.type2",
        "payload": {
            "column1": "12345",
            "column2": "description",
            "column3": ""
        }
    }    
]
This is my code but is not removing and I think it's because of json structure

a = [d for d in payload if "" not in d.values()]

print(type(a))
<class 'list'>
Does anyone knows how can I solve it?
Reply


Messages In This Thread
Remove empty keys in a python list - by python_student - Jan-11-2022, 05:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python automation: Sending clicks/keys to VMware virtual machine from host olips784 0 328 May-18-2025, 06:28 PM
Last Post: olips784
  remove duplicates from dicts with list values wardancer84 27 6,643 May-27-2024, 04:54 PM
Last Post: wardancer84
  what to return for an empty list Skaperen 2 1,344 May-24-2024, 05:17 PM
Last Post: Skaperen
  unable to remove all elements from list based on a condition sg_python 3 1,828 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Code with empty list not executing adeana 9 6,116 Dec-11-2023, 08:27 AM
Last Post: buran
  Remove numbers from a list menator01 4 3,957 Nov-13-2022, 01:27 AM
Last Post: menator01
  set.difference of two list gives empty result wardancer84 4 2,722 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Updating nested dict list keys tbaror 2 2,036 Feb-09-2022, 09:37 AM
Last Post: tbaror
  displaying empty list vlearner 5 3,144 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove an item from a list contained in another item in python CompleteNewb 19 9,256 Nov-11-2021, 06:43 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020