Python-从词典列表中删除具有空值的字典 [英] Python - Remove dicts with empty values from list of dictionaries

查看:93
本文介绍了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.

这篇关于Python-从词典列表中删除具有空值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆