One liner to determine if dictionary values are all empty lists or not

前端 未结 6 886
-上瘾入骨i
-上瘾入骨i 2021-01-01 22:24

I have a dict as follows:

someDict = {\'a\':[], \'b\':[]}

I want to determine if this dictionary has any values which are not empty lists.

6条回答
  •  隐瞒了意图╮
    2021-01-01 23:11

    Not falsey or not empty lists:

    Not falsey:

    any(someDict.values())
    

    Not empty lists:

    any(a != [] for a in someDict.values())
    

    or

    any(map(lambda x: x != [], someDict.values()))
    

    Or if you are ok with a falsey return value:

    filter(lambda x: x != [], someDict.values())
    

    Returns a list of items that are not empty lists, so if they are all empty lists it's an empty list :)

提交回复
热议问题