xxxxxxxxxx
my_dict = {} # Example dictionary
# Method 1: Using the len() function to check if the dictionary is empty
if len(my_dict) == 0:
print("Dictionary is empty")
else:
print("Dictionary is not empty")
# Method 2: Using the bool() function to check if the dictionary is empty
if bool(my_dict):
print("Dictionary is not empty")
else:
print("Dictionary is empty")
# Method 3: Using the `not` keyword to check if the dictionary is empty
if not my_dict:
print("Dictionary is empty")
else:
print("Dictionary is not empty")
xxxxxxxxxx
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"
xxxxxxxxxx
# Initialize a dictionary
my_dict = {}
# Check if the dictionary is empty
if not bool(my_dict):
print("Dictionary is empty")
else:
print("Dictionary is not empty")
xxxxxxxxxx
empty_dict = {}
if empty_dict:
print('Dictionary is not empty!')
else:
print('Dictionary is empty!')
xxxxxxxxxx
empty_dict = {}
if empty_dict:
print('Dictionary is not empty!')
else:
print('Dictionary is empty!')
xxxxxxxxxx
empty_dict = {}
if empty_dict:
print('Dictionary is not empty!')
else:
print('Dictionary is empty!')
xxxxxxxxxx
empty_dict = {}
if empty_dict:
print('Dictionary is not empty!')
else:
print('Dictionary is empty!')
xxxxxxxxxx
sample_dict = {} # Example dictionary
# Method 1: Using the len() function
if len(sample_dict) == 0:
print("Dictionary is empty") # Prints if the dictionary is empty
else:
print("Dictionary is not empty") # Prints if the dictionary is not empty
# Method 2: Using the bool() function
if not bool(sample_dict):
print("Dictionary is empty") # Prints if the dictionary is empty
else:
print("Dictionary is not empty") # Prints if the dictionary is not empty