I have a nested dictionary.
x = {
'Jack': {'Age': '34', 'Location' : 'Chicago'}, 'Mary': {'Insurance': 'fullPaid', 'Age' : '27', 'Location' : 'London'}
}
What im trying to do is swap dict's keys so output dictionary would look like this:
{
'Age':{'Jack' : '34', 'Mary' : '27'}, 'Location': {'Jack' : 'Chicago', 'Mary' : 'London'}, 'Insurance': {'Mary' : 'fullPaid'}
}
So far i got to this point:
y = defaultdict(list)
for key, values in x.items():
for value in values:
y[value].append(key)
where output is
defaultdict(<class 'list'>, {'Age': ['Jack', 'Mary'], 'Location': ['Jack', 'Mary'], 'Insurance': ['Mary']})