I change the final structure by a more logical:
{'state1': {'city1': ['dict1', 'dict2']}, 'state2': {'City2': ['dict3']}}
and the code:
dir_dict = {}
for one in objects:
state = one.dir.city.state.name
city = one.dir.city.name
address = one.dir.address
if state not in dir_dict:
dir_dict[state] = {}
if city not in dir_dict[state]:
dir_dict[state][city] = []
dir_dict[state][city].append(address)
I still implement code @ Eric
1 Answer