Python Practice › Dictionary Programs › Program 79
Dictionary with the most keys
Write a Python program to find which of two dictionaries has more keys.
Input
{"name": 1, "age": 2}, {"name": 1, "age": 2, "course": 3, "institute": 4}Output
2nd dictionary has maximum keys, 4
Approach
- Compare
len().
Solution
a = {"name": 1, "age": 2}b = {"name": 1, "age": 2, "course": 3, "institute": 4}bigger = max([("1st", a), ("2nd", b)], key=lambda p: len(p[1]))print(f"{bigger[0]} dictionary has maximum keys, {len(bigger[1])}")Output
2nd dictionary has maximum keys, 4
Learn the concept
New to this topic? Read the lesson, then come back.