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
If I have A={1:{2:3,4:2},2:{}}
, and I want to add elements to A[1]
. How to add here?
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: {}}
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2019-10-29 07:48:31 +0100
Seen: 224 times
Last updated: Nov 05 '19