Skip to content
Advertisement

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

Advertisement

Answer

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}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement