Create a single dict from a dict of dict

I have a dictionary of dictionaries in python.

I want to convert it into one dictionary that uses the key as the values of the earlier dictionary

{
('user1'): {27: 'A'}, 
('user2'): {23: 'B', 27: 'A'}, 
('user3'): {27: 'A', 23: 'B'}
}

I want to convert it into

{
('27: 'A'): {'user1','user2','user3'}, 
('23: 'B'): {'user2'}, 
}

1 answer

  • answered 2020-10-16 05:07 Krittam Kothari

    Short Answer : What you are trying to achieve is actually not possible.

    Short Reason : Dictionary is not hashable

    Explanation : a python dictionary is basically a key value pair where the key should be hashable. Since a dictionary itself is not a hashable type it can't be the key of another dictionary. For example

    {{1:2}:3}
    

    Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'dict'