The program creates a dictionary student with five key-value pairs. Some values are None, indicating that the information is missing or not applicable.
The second line of code creates a new dictionary student using a dictionary comprehension. The comprehension iterates over the key-value pairs of the original dictionary student using the items method. For each pair, a condition is checked to see if the value is None. If the value is not None, the key-value pair is included in the new dictionary. If the value is None, the pair is excluded.
Finally, the original dictionary and the new dictionary (after dropping the key-value pairs with None values) are printed using the print function. In summary, the program demonstrates how to remove key-value pairs with None values from a dictionary in Python using a dictionary comprehension.
student = {"Name": "Pooja", "Age":23, "Gender": None, "Mark":488, "City": None} print("Before Dropping :",student) student = {key:value for (key, value) in student.items() if value is not None} print("After Dropping :",student)
Before Dropping : {'Name': 'Pooja', 'Age': 23, 'Gender': None, 'Mark': 488, 'City': None} After Dropping : {'Name': 'Pooja', 'Age': 23, 'Mark': 488}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions