Keys with a given value

EasyDictionary Programs · 83 of 94

Write a Python program to find all keys that have a given value.

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

Approach

  1. Filter items().

Solution

Python
d = {"a": 19, "b": 20, "c": 21, "d": 20}print([k for k, v in d.items() if v == 20])
Output
['b', 'd']
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs