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

前端 未结 6 684
庸人自扰
庸人自扰 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 19:59

    One more option is the following (without creating a new dict):

    for e in [k for k,v in default.iteritems() if len(v) == 0]: default.pop(e)
    

提交回复
热议问题