how to use key as index in nested dictionary?
If I have A={1:{2:3,4:2},2:{}}, and I want to add elements to A[1]. How to add here?
add a comment
Alternative solution:
sage: D={1:{2:3,4:2},2:{}}
sage: D[1].update({5:'smile'})
sage: D
{1: {2: 3, 4: 2, 5: 'smile'}, 2: {}}
Possibly more readable...
Like that
sage: A={1:{2:3,4:2},2:{}}
sage: A[1][5]='smile'
sage: A
{1: {2: 3, 4: 2, 5: 'smile'}, 2: {}}
Asked: 2019-10-29 07:48:31 +0100
Seen: 321 times
Last updated: Nov 05 '19
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.