Python-从词典列表中删除具有空值的字典
问题描述:
我想删除具有空值的字典元素列表.
I'd like to remove the list of dictionaries elements that have empty values.
对于以下情况:
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": ""}]
将返回以下新的格言:
[{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}]
我尝试过:
{k:v for k,v in dict if not v}
但是我得到了:
ValueError: too many values to unpack (expected 2)
答
尝试一下:
print([d for d in arr if all(d.values())])
请勿使用 dict
作为名称:您将覆盖Python的名称.在您的情况下,它不是字典,而是字典数组.
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.