This dict structure :
data = {
'a': { 'category': ['c', 'd'] },
'b': { 'category': ['c', 'd'] }
}
should become this dict structure:
data = {
'c' : ['a', 'b'],
'd' : ['a', 'b']
}
I have following approach:
for key, value in data.items():
if isinstance(value, dict):
if 'category' in value:
for cat in value['category']:
if cat in categories:
categories[cat].append(key)
else:
categories[cat] = [key]
I want to know if there is any way to simplify my approach. I am using python 3.5