Python: An elegant way to delete empty lists from Python dictionary

前端 未结 6 645
庸人自扰
庸人自扰 2021-01-11 19:30

I have a dictionary as:

default = {\'a\': [\'alpha\'], \'b\': [\'beta\',\'gamma\'], \'g\': []}

I wish to eliminate the empty values as:

6条回答
  •  心在旅途
    2021-01-11 20:00

    dict((k, v) for k, v in default.iteritems() if v)
    

    This filters all items which are not empty strings, empty dict/tuple/list.

提交回复
热议问题