Python: How to check if a nested list is essentially empty?

前端 未结 7 1643
栀梦
栀梦 2020-12-29 05:56

Is there a Pythonic way to check if a list (a nested list with elements & lists) is essentially empty? What I mean by

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 06:06

    I have combined the use of isinstance() by Ants Aasma and all(map()) by Stephan202, to form the following solution. all([]) returns True and the function relies on this behaviour. I think it has the best of both and is better since it does not rely on the TypeError exception.

    def isListEmpty(inList):
        if isinstance(inList, list): # Is a list
            return all( map(isListEmpty, inList) )
        return False # Not a list
    

提交回复
热议问题