Python Practice › If-Else Programs › Program 47
Check if a value is a dictionary
Write a Python program to check whether a value is a dictionary.
Input
{"name": "Virat", "sport": "cricket"}Output
It is a dictionary with keys ['name', 'sport']
Approach
- Use
isinstance(value, dict).
Solution
player = {"name": "Virat", "sport": "cricket"}if isinstance(player, dict): print("It is a dictionary with keys", list(player))else: print("It is not a dictionary")Output
It is a dictionary with keys ['name', 'sport']
Line by line
Line 3
list(player) lists the dictionary’s keys.
Learn the concept
New to this topic? Read the lesson, then come back.