Python: check dict keys for double value and assign new dict
Customization
I have a dictionary containing districts and postal codes, for example.
d={
'Center':['A1', 'B1','C1', 'D1'],
'West':['A1', 'B2','C2', 'D2'],
⋮
'North':['A1', 'B2','C3', 'D3'],
}
Problem
Some postal codes are found in several areas, for example. A1
is in Center
, West
and North
and B2
in West
and North
.
I want to check which postal codes are in multiple areas and then create a reverse dictionary with those postal codes and areas ie.
vice_versa = {
'A1':['Center', 'West', 'North']
⋮
'B2':['West', 'North']
}
(Semi) Code bye
vice_versa={}
for key in list(d.keys()):
for x in d[key]:
if x in d[~key]:
vice_versa[x] = key, ~key
There are two problems:
- How to iterate over other keys eg. if
key='Center'
, then how to iterate over~key='West'
and~key='North'
. - How to assign all keys
vice_versa
, eg. how to assign'Center'
,'North'
and'West'
onvice_versa['A1']
.
For clarity, vice_versa
should only contain "duplicate" postal codes, for example. A1
and B2
and not other postal codes in the example (eg not C1
).
source to share
Based on what you are describing here, you need to rearrange the dictionary. In my opinion, it is better to use for this defaultdict
(which is a subclass dict
, so all dictionary operations are supported):
from collections import defaultdict
vice_versa = defaultdict(list)
for region,postals in d.items():
for postal in postals:
vice_versa[postal].append(region)
In the second phase, we can filter out mails with only one region, for example using dictionary comprehension and putting the result back into the vanilla dictionary:
vice_versa = {k:v for k,v in vice_versa.items() if len(v) > 1}
Based on your example input, this gives:
>>> {k:v for k,v in vice_versa.items() if len(v) > 1}
{'B2': ['North', 'West'], 'A1': ['Center', 'North', 'West']}
source to share