Drop empty items

EasyDictionary Programs · 50 of 94

Write a Python program to drop items whose value is None.

Advertisement
Input
{"m1": 40, "m2": 50, "m3": None}
Output
{'m1': 40, 'm2': 50}

Approach

  1. Keep pairs where v is not None.

Solution

Python
d = {"m1": 40, "m2": 50, "m3": None}print({k: v for k, v in d.items() if v is not None})
Output
{'m1': 40, 'm2': 50}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment