Remove keys with large values

EasyDictionary Programs · 68 of 94

Write a Python program to remove keys whose values are greater than n.

Advertisement
Input
{"sqa": 3, "tools": 5, "python": 7}, n = 6
Output
{'sqa': 3, 'tools': 5}

Approach

  1. Keep v <= n.

Solution

Python
d = {"sqa": 3, "tools": 5, "python": 7}n = 6print({k: v for k, v in d.items() if v <= n})
Output
{'sqa': 3, 'tools': 5}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment