nmh
nmh

Reputation: 501

Python - Remove dicts with empty values from list of dictionaries

I'd like to remove the list of dictionaries elements that have empty values.

So for the following:

dict = [{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}, {"end": "", "item": "", "start": ""},  {"end": "", "item": "", "start": ""}, {"end": "", "item": "", "start": ""}]

would return a new dict of:

[{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}]

I've tried:

{k:v for k,v in dict if not v}

but I'm getting:

ValueError: too many values to unpack (expected 2)

Upvotes: 1

Views: 1424

Answers (1)

Danil Speransky
Danil Speransky

Reputation: 30453

Try this:

print([d for d in arr if all(d.values())])

Don't use dict as a name: you override Python's one. And in your case it's not a dictionary, it is an array of dictionaries.

Upvotes: 6

Related Questions