I have a defaultdict of list like this
{'A':[1,2,3,3,2],'B':[1,2,3,3,2]}
Need to remove duplicates in the values only. The dict should be like
{'A':[1,2,3],'B':[1,2,3]}
Tried
dict((k, tuple(v)) for k, v in list_of_value.items())
Not helping much.
CodePudding user response:
Just change the tuple to Set. As the property of set is to make it unique. The change it back to list.
list_of_value = {'A':[1,2,3,3,2],'B':[1,2,3,3,2]}
dict((k, list(set(v))) for k, v in list_of_value.items())
CodePudding user response:
you can use the same dict to save to memoy
dict_t = {'A':[1,2,3,3,2],'B':[1,2,3,3,2]}
for key,val in dict_t.items():
dict_t[key] = list(set(dict_t[key]))
#output {'A': [1, 2, 3], 'B': [1, 2, 3]}
CodePudding user response:
d = {'A':[1,2,3,3,2],'B':[1,2,3,3,2]}
u = {k : list(set(d[k])) for k, v in d.items()}
