Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to delete an empty list in an array object in python?

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}
                ]
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading