Python Practice › Dictionary Programs › Program 51
Filter by value
Write a Python program to keep entries with values greater than 40.
Input
{"alex": 50, "john": 45, "Robert": 30}Output
{'alex': 50, 'john': 45}Approach
- Dict comprehension with a condition.
Solution
d = {"alex": 50, "john": 45, "Robert": 30}print({k: v for k, v in d.items() if v > 40})Output
{'alex': 50, 'john': 45}Learn the concept
New to this topic? Read the lesson, then come back.