Keys with the shortest lists

EasyDictionary Programs · 73 of 94

Write a Python program to find the keys whose list values are the shortest.

Advertisement
Input
{"a": [10, 12], "b": [10], "c": [10, 20, 30, 40], "d": [20]}
Output
['b', 'd']

Approach

  1. Find the minimum length, then keep keys with that length.

Solution

Python
d = {"a": [10, 12], "b": [10], "c": [10, 20, 30, 40], "d": [20]}shortest = min(len(v) for v in d.values())print([k for k, v in d.items() if len(v) == shortest])
Output
['b', 'd']
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs