31_Is_Dictionary_full_or_empty.py


Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...


Code Snippet

def main():
    #create a dictionary to store teacher's details - such as their subject, hobby, school house, and more
    d1={"Name": "Mr Moose","Subject": "Philosophy","Hobby":"Chess","School House":"Red House"}
    d2={} #empty dictionary

    
    print(bool(d1)) #dictionaries with data will return TRUE
    print(bool(d2)) #empty dictionaries return FALSE

   
    while bool(d2): #change this to d1, and the while loop will execute!
        print("Do stuff in the program")
    print("Nothing in the Dictionary - cannot proceed, sorry!")
   
   

main()
                    

Try it yourself