[Solved] how to make condition with if to check type of variables in python?


If your object isn’t a dict but could be converted to a dict easily (e.g. list of pairs), you could use:

def make_a_dict(some_object):
    if isinstance(some_object, dict):
        return some_object
    else:
        try:
            return dict(some_object)
        except:
            return {'return': some_object}

print(make_a_dict({'a': 'b'}))
# {'a': 'b'}
print(make_a_dict([(1,2),(3,4)]))
# {1: 2, 3: 4}
print(make_a_dict(2))
# {'return': 2}

solved how to make condition with if to check type of variables in python?