In this python dictionary program, we will drop empty Items from a given dictionary.
Steps to solve the program
- Take a dictionary as input and create an empty dictionary.
- Using a for loop and an if-else statement add the key-value pairs from the input dictionary whose value is not empty to the empty dictionary.
- Print the output.
dict1 = {"m1":40,"m2":50,"m3":None}
dict2 = {}
for key,val in dict1.items():
if val != None:
dict2[key] = val
dict2
Output :
{'m1': 40, 'm2': 50}