How to count distinct keys in a Python dictionary?

I’ve worked with Python dictionaries a lot, and one of the simplest ways is by using Python’s built-in len() function. It’s straightforward and efficient when counting keys in a dictionary. Here’s how it works:

data = {"apple": 3, "banana": 5, "cherry": 2}
distinct_key_count = len(data)
print(distinct_key_count)  # Output: 3

Explanation: The len() function calculates the number of keys directly. Since dictionaries inherently store unique keys, this gives you the exact count of distinct keys. A classic and quick way to handle a python dict count scenario.