Python Practice › Dictionary Programs › Program 88
Delete several keys
Write a Python program to delete a list of keys from a dictionary.
Input
{"a": 19, "b": 20, "c": 21, "d": 20, "e": 50}, keys ["a", "d", "e"]Output
{'b': 20, 'c': 21}Approach
pop(k, None)for each key.
Solution
d = {"a": 19, "b": 20, "c": 21, "d": 20, "e": 50}for k in ["a", "d", "e"]: d.pop(k, None)print(d)Output
{'b': 20, 'c': 21}Learn the concept
New to this topic? Read the lesson, then come back.