Python: Checking if a 'Dictionary' is empty doesn't seem to work

前端 未结 8 585
旧巷少年郎
旧巷少年郎 2020-12-02 04:27

I am trying to check if a dictionary is empty but it doesn\'t behave properly. It just skips it and displays ONLINE without anything except of display the m

8条回答
  •  误落风尘
    2020-12-02 04:45

    Simple ways to check an empty dict are below:

            a= {}
    
        1. if a == {}:
               print ('empty dict')
        2. if not a:
               print ('empty dict')
    

    Although method 1st is more strict as when a = None, method 1 will provide correct result but method 2 will give an incorrect result.

提交回复
热议问题