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

前端 未结 8 568
旧巷少年郎
旧巷少年郎 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:38

    Here are three ways you can check if dict is empty. I prefer using the first way only though. The other two ways are way too wordy.

    test_dict = {}
    
    if not test_dict:
        print "Dict is Empty"
    
    
    if not bool(test_dict):
        print "Dict is Empty"
    
    
    if len(test_dict) == 0:
        print "Dict is Empty"
    

提交回复
热议问题