Re: strange dict issue
On Mon, Jan 12, 2009 at 7:24 AM, Heston James - Cold Beans heston.ja...@coldbeans.co.uk wrote: Ok, this feels like a horribly noobish question to ask guys but I can't figure this one out. I have code which looks like this: print this_config[1] this_adapter_config[*name*] = this_config[1][*NAME*] Now, the print statement gives me the following: {'value': 'Route66', 'key': 'NAME'} Yet when the second line of my code throws an error saying the key 'NAME' doesn't exist. Any ideas on what's going on, seems quite senseless to me!? Thanks, Heston A dict stores key/value pairs. When you see the print of the dict, it shows you {key1:value1, key2:value2}. So your dict has two keys ('value' and 'key') that map to two values ('Route66' and 'Name' respectively). 'Name' is a value in the dict, not a key, so you can't use that syntax to get it.. In order for this code to work, the dictionary would have to be {'NAME' : 'Route66'} or you would have to use this_config[1]['value'] -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: strange dict issue
Heston James - Cold Beans wrote: Ok, this feels like a horribly noobish question to ask guys but I can’t figure this one out. I have code which looks like this: print this_config[1] this_adapter_config[/name/] = this_config[1][/NAME/] Now, the print statement gives me the following: {'value': 'Route66', 'key': 'NAME'} Yet when the second line of my code throws an error saying the key ‘NAME’ doesn’t exist. Any ideas on what’s going on, seems quite senseless to me!? Thanks, Heston -- http://mail.python.org/mailman/listinfo/python-list 'NAME' is the value, and 'key' is the key. Your dictionary has two keys mapped to two values: 'value' - 'Route66' 'key' - 'NAME' You need to lookup the values by their key. Does that make sense? Thanks, Gary M. Josack -- http://mail.python.org/mailman/listinfo/python-list
RE: strange dict issue
Gary, Ben, Thanks for your replies, that makes perfect sense. This is a dict generated by the ZSI web service library and looks different to what I expect. :-( I'll work on your suggestions, thanks again for your help. Heston -- http://mail.python.org/mailman/listinfo/python-list
Re: strange dict issue
James First off, the computer is always right :-) {'value': 'Route66', 'key': 'NAME'} Yet when the second line of my code throws an error saying the key 'NAME' doesn't exist. If you look carefully the key NAME indeed does not exist. The dictionary contains 2 key-value pairs. This should clear things out: *key * *value *'value' 'Route66' 'key' 'NAME' Get it? NAME is a value, not a key. You have used the string 'key' itself as a dictionary key. My advice would be to not use such conflicting names. Regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list