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
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: {}}
can i use this using for loop?
Sure you can use a loop; for instance:
sage: A={1:{2:3,4:2},2:{}}
sage: for key, value in A.items():
....: value[5] = key^2
....:
sage: A
{1: {2: 3, 4: 2, 5: 1}, 2: {5: 4}}
Thank You.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 5 years ago
Seen: 243 times
Last updated: Nov 05 '19