Answer the question
In order to leave comments, you need to log in
How to make a multidimensional dictionary with grouping in Python (see example)?
Let me explain with an example. There is json:
{'city':'Moscow', 'manager':'Igor'},
{'city':'Kazan', 'manager':'Valera'},
{'city':'Moscow', 'manager':'Olga'},
{'city':'Kazan', 'manager':'Natalia'},
{'city':'Moscow', 'manager':'Oleg'},
'Moscow': ['Igor','Olga','Oleg'],
'Kazan': ['Valera','Natalia']
Answer the question
In order to leave comments, you need to log in
res = {}
for d in ({'city': 'Moscow', 'manager': 'Igor'},
{'city': 'Kazan', 'manager': 'Valera'},
{'city': 'Moscow', 'manager': 'Olga'},
{'city': 'Kazan', 'manager': 'Natalia'},
{'city': 'Moscow', 'manager': 'Oleg'}):
res.setdefault(d['city'], []).append(d['manager'])
print(res)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question