items = [{'id': 1, 'language': 'English', 'name': 'Sarah', 'description': 'Blah blah'}, {'id': 2, 'language': 'English', 'name': 'Jessica', 'description': 'More blah'}]
d = {}
for item in items:
language = item['language']
id = item['id']
name = item['name']
description = item['description']
d[language][id] = {'name': name, 'description': description}
print(d)
I'm expecting to see in output:
{'English': {1:{'name': 'Sarah', 'description': 'Blah blah'}, 2:{'name': 'Jessica', 'description': 'More blah'}}}
But unfortunately I'm getting KeyError:
So, the question is how to update/append value in nested dictionary? What I'm doing wrong?