I am trying to remove the particular value from the list of key. I got Type error. I wrote function to add and remove the key. need to remove a particular value from the key
class my_dict(dict):
def add(self, key, value):
self.setdefault(key, []).append(value)
def remove_key(self, key):
del self[key]
#Error part
def remove_value(self,key,value):
for value in dict.values():
if v in value:
value.remove(v)
dict_obj = my_dict()
dict_obj.add('key1', 'value1')
dict_obj.add('key2', 'value2')
dict_obj.add('key1', 'value3')
print(dict_obj)
>>{'key1': ['value1', 'value3'], 'key2': ['value2']}
dict_obj.remove_value('key1','value3')
print(dict_obj)
My Out
TypeError: descriptor 'values' of 'dict' object needs an argument
Desired Output
>>{'key1': ['value1'], 'key2': ['value2']}
self.values()instead ofdict.values()? Or at leastdict.values(self). The methodvalues()is an instance method ondict, and you're trying to call it like a class method.vwas supposed to be, and where did you do so?self[key].valueifvalue in self[key]