Efficient way to remove keys with empty strings from a dict

前端 未结 17 1280

I have a dict and would like to remove all the keys for which there are empty value strings.

metadata = {u\'Composite:PreviewImage\': u\'(Binary data 101973          


        
17条回答
  •  情书的邮戳
    2020-11-27 13:19

    It can get even shorter than BrenBarn's solution (and more readable I think)

    {k: v for k, v in metadata.items() if v}
    

    Tested with Python 2.7.3.

提交回复
热议问题