0

I'm looping through a data set, and updating a dictionary as needed. As a precursor to the dictionary update, I have a "default" dictionary "template" like so:

databody_item = {'name': 'Kermit',
                'interests': {
                                'test1':True,
                             },
                }

I want to update the interests dictionary inside of the databody_item dictionary, so I run:

databody_item.update({'interests':{'test2':True}})

This however cleared the entire interests dict, leaving me with this:

{'interests': {'test2': True}, 'name': 'Kermit'}

However, I need the dictionary to be updated like this:

{'interests': {'test1': True, 'test2': True}, 'name': 'Kermit'}
  • What you want to do is `databody_item['interests'].update({'test2': True})`. Or just `databody_item['interests']['test2'] = True`. – abarnert Apr 19 '18 at 17:18
  • If you explain a bit more about what you're trying to do, and why you thought `update` was necessary for the task, we can probably either find a dup or write a complete answer that explains things to you better. – abarnert Apr 19 '18 at 17:19
  • @abarnert thanks so much. Your first suggestion helped. I searched for dictionary inside of dictionary, but never thought to reference it as a nested dictionary. The question this one was marked as a duplicate of resolved the issue, as well does your response. – unknown6708 Apr 19 '18 at 17:27

0 Answers0