python - how to get a defautdict(set) like this form : {str : { str : int } } -
i got dictionary this
{('a','b'):3, ('b','c'): 2, ('a','c'): 5} i want convert form :
{'b': {'c': 2}, 'a': {'c': 5, 'b': 3}} that means have build dictionary form:
d:{(str1,str2):int} --> {str1 : { str2 : int } }
you not want defaultdict based on set based on dict instead.
dic = {('a','b'):3, ('b','c'): 2, ('a','c'): 5} dic2 = defaultdict(dict) j,k in dic: dic2[j][k] = dic[(j,k)] ## dic2 = defaultdict(<type 'dict'>, {'a': {'c': 5, 'b': 3}, 'b': {'c': 2}}) on side note, d.keys() iterable, there no need call list(d.keys()). in fact, iterate on keys of dictionary d, best call for key in d: