Check if a value is a dictionary

EasyIf-Else Programs · 47 of 64

Write a Python program to check whether a value is a dictionary.

Advertisement
Input
{"name": "Virat", "sport": "cricket"}
Output
It is a dictionary with keys ['name', 'sport']

Approach

  1. Use isinstance(value, dict).

Solution

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

Advertisement

Learn the concept

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

More if-else programs

← All 64 If-Else Programs