class MyDict(dict): def __init__(self): self.temp_list = [] super(MyDict,self).__init__() def __setitem__(self, key, value): self.temp_list.append(key) super(MyDict,self).__setitem__(key, value) def __str__(self): my_list = [] for x in self.temp_list: value = self.get(x) my_list.append("'{}':{}".format(x,value)) temp_dict = '{'+','.join(my_list)+'}' return temp_dict obj = MyDict() obj["k1"] = 123 obj["k2"] = 456 obj["k3"] = 789 print(obj)