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?
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: {}}
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...
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 +0200
Seen: 221 times
Last updated: Nov 05 '19