Filter by value

EasyDictionary Programs · 51 of 94

Write a Python program to keep entries with values greater than 40.

Advertisement
Input
{"alex": 50, "john": 45, "Robert": 30}
Output
{'alex': 50, 'john': 45}

Approach

  1. Dict comprehension with a condition.

Solution

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

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment