Python - 从词典列表中删除带有空值的dicts

时间:2017-09-04 12:24:36

标签: python list dictionary

我想删除具有空值的字典元素列表。

所以对于以下内容:

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)

1 个答案:

答案 0 :(得分:5)

试试这个:

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

不要使用dict作为名称:覆盖Python的名称。在你的情况下,它不是字典,而是一系列字典。