Home > Back-end >  how to delete an empty list in an array object in python?
how to delete an empty list in an array object in python?

Time:07-11

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

CodePudding user response:

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}
  • Related