Python Practice › Dictionary Programs › Program 73
Keys with the shortest lists
Write a Python program to find the keys whose list values are the shortest.
Input
{"a": [10, 12], "b": [10], "c": [10, 20, 30, 40], "d": [20]}Output
['b', 'd']
Approach
- Find the minimum length, then keep keys with that length.
Solution
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']
Learn the concept
New to this topic? Read the lesson, then come back.