Delete several keys

EasyDictionary Programs · 88 of 94

Write a Python program to delete a list of keys from a dictionary.

Advertisement
Input
{"a": 19, "b": 20, "c": 21, "d": 20, "e": 50}, keys ["a", "d", "e"]
Output
{'b': 20, 'c': 21}

Approach

  1. pop(k, None) for each key.

Solution

Python
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.

More dictionary programs

← All 94 Dictionary Programs