Python Practice › Dictionary Programs › Program 68
Remove keys with large values
Write a Python program to remove keys whose values are greater than n.
Input
{"sqa": 3, "tools": 5, "python": 7}, n = 6Output
{'sqa': 3, 'tools': 5}Approach
- Keep
v <= n.
Solution
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.