Dictionary with the most keys

EasyDictionary Programs · 79 of 94

Write a Python program to find which of two dictionaries has more keys.

Advertisement
Input
{"name": 1, "age": 2}, {"name": 1, "age": 2, "course": 3, "institute": 4}
Output
2nd dictionary has maximum keys, 4

Approach

  1. Compare len().

Solution

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

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs