How to check if a dictionnary or dictionary's element is empty?

I have this:

d = {
    "fruits" : {
        "label" : "orange",
        "cost" : 3.65

I would like to check if the dictionary d is empty, or if d["fruits"] is empty before we get the values of the dictionaries

We can use the list.keys() or list.items() method to check if the dictionary is empty…

if len(d.keys()) < 1:
    print ("dictionary is empty")


if len(d['fruits'].keys()) < 1:
    print ("fruits dictionary is empty")
1 Like