Monday, July 25, 2022

[FIXED] how to delete an empty list in an array object in python?

Issue

I have this as data


store_list = {
    "ECG 12D":[],
    "ETT Adulte":[
                    {"series":"Série sans label","modality":"OT","count":48},
                    {"series":"Série sans label","modality":"SR","count":47},
                    {"series":"Série sans label","modality":"ETT","count":43}
                ]
    }

I would like to know how to remove ECG12D in my data for example ? Get this !

store_list = {
    "ETT Adulte":[
                    {"series":"Série sans label","modality":"OT","count":48},
                    {"series":"Série sans label","modality":"SR","count":47},
                    {"series":"Série sans label","modality":"US","count":43}
                ]
    }

Solution

Try this:

{k: v for k, v in store_list.items() if v}

UPDATE:

Somebody suggested edit:

{k: v for k, v in store_list.items() if v == []}

It's actually unnecessary, because python interprets empty list as False value.

And also if you don't want to use syntax sugar, the better way, as for me, will be:

{k: v for k, v in store_list.items() if len(v) > 0}


Answered By - eightlay
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.