I have a dictionary that maps the territory_code
to what productIds
are available:
items = {'US': set(123, 948, 200), 'AG': set(132, 123), 'AZ': set(123)}
I would like to reverse the mapping so it gives me the items and maps those to the territory. It should give me:
{123: set('US', 'AZ', 'AG'), 948: set('US'), 200: set('US'), 132: set('AG')}
How would I do this reversing?
You can try the brute force way.
Logic - For every value in the old dictionary create a new key in the new dictionary and add the old key as the new value
Bare code without REPL arrows
A small note here. If you are allowed imports then you can use
defaultdict
from collectionsIn this way you can avoid using the
if
clause in between.