["python","dictionary"]
How to check if a variable is a dictionary in Python?

How would you check if a variable is a dictionary in Python? For example, I'd like it to loop through the values in the dictionary until it finds a dictionary. Then, loop through the one it finds: dict = {'abc': 'abc', 'def': {'ghi': 'ghi', 'jkl': 'jkl'}} for k, v in dict.items(): if ###check if v is a dictionary: for k, v in v.iteritems(): print(k, ' ', v) else: print(k, ' ', v)
Use isinstance(ele, dict), which will return true for dict objects as well as subclasses of dict, such as OrderedDict and defaultdict: d = {'abc': 'abc', 'def': {'ghi': 'ghi', 'jkl': 'jkl'}} for element in d.values(): if isinstance(element, dict): for k, v in element.items(): print(k, ' ', v) You can do if type(ele) is dict if you want to check strictly for instances of dict and not subclasses of it, though this is generally not recommended.