[Tutor] updating a dictionary

Chris Stinemetz chrisstinemetz at gmail.com
Sat Feb 21 00:47:29 CET 2015


On Fri, Feb 20, 2015 at 4:51 PM, Mark Lawrence <breamoreboy at yahoo.co.uk>
wrote:

> On 20/02/2015 17:56, Chris Stinemetz wrote:
>
> Please don't top post as it makes long threads difficult if not impossible
> to follow, thanks.
>
>  I am getting closer. I think I have figured out the logic. I just have a
>> quick question. How do you access key:values in a nested dictionary?
>>
>> MOL02997_C': [{'2': '0', '7': '0', '8': '0', '9': '0'}]}
>>
>
>
>> say I want to access the key:value 8:0
>>
>> print dict['MOL02997_C']['8'] doesn't seem to work.
>>
>
> "doesn't seem to work" doesn't tell us much, so normally you would post
> your code and the full traceback that you get.  However what you have seems
> to be a dictionary that you've called dict, hence overriding the Python
> built-in name.  This isn't illegal but it's certainly frowned upon.  For
> the key 'MOL02997_C' you have a list which holds one dict which contains a
> value '8' amongst others.  Hence:-
>
> >>> mystruct = {'MOL02997_C': [{'2': '0', '7': '0', '8': '0', '9': '0'}]}
> >>> mystruct
> {'MOL02997_C': [{'7': '0', '8': '0', '2': '0', '9': '0'}]}
> >>> mystruct['MOL02997_C']
> [{'7': '0', '8': '0', '2': '0', '9': '0'}]
> >>> mystruct['MOL02997_C'][0]
> {'7': '0', '8': '0', '2': '0', '9': '0'}
> >>> mystruct['MOL02997_C'][0]['8']
> '0'
>
> Got that?
>
>
>
​
Thank you Mark.

I understand what you are explaining to me but I am not sure why every
instance of the key 8:value changes when I assign a new value to it.

I am expecting only vals['KSL04523_A'][0]['8'] value to change to 55.55 but
as you can see bellow all rows in the dictionary are changes for key 8:

Thank you in advance

>>> vals['KSL04523_A']
[{'7': '0', '9': '0', '8': '0', '2': '0'}]
>>> vals['KSL04523_A'][0]
{'7': '0', '9': '0', '8': '0', '2': '0'}


>>> vals['KSL04523_A'][0]['8']
'0'


>>> vals['KSL04523_A'][0]['8'] = 55.55
>>> pprint.pprint(vals)
{'CELL_': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04514_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04514_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04515_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04515_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04515_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04516_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04516_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04516_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04517_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04517_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04517_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04519_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04519_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04519_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04520_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04520_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04520_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04521_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04521_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04521_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}],
 'KSL04523_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}]}​


More information about the Tutor mailing list