Re: key not found in dictionary

2006-08-23 Thread Sion Arrowsmith
Chaz Ginger <[EMAIL PROTECTED]> wrote: >KraftDiner wrote: >> desc = self.numericDict[k][2] >> KeyError: 589824 < This is the error that is being produced, >As stated you can wrap the access in the try - except - else statement, >as in > >try: > foo['bar'] >except : > # Handle the erro

Re: key not found in dictionary

2006-08-22 Thread Fredrik Lundh
Ben Finney wrote: > In the specific use case of wanting a default value when a dictionary > doesn't have a particular key, you can also use this: > > >>> foo = {0: "spam", 1: "eggs", 7: "beans"} > >>> for key in [1, 2, 7]: > ... desc = foo.get(key, None) usually spelled de

Re: key not found in dictionary

2006-08-22 Thread Simon Forman
> Or you can use the has_key() and test it first. For example > > if foo.has_key('bar'): > print 'we have it' > else : > print 'we don't have bar.' > Nowadays you can also say: if 'bar' in foo: # do something -- http://mail.python.org/mailman/listinfo/python-list

Re: key not found in dictionary

2006-08-22 Thread Ben Finney
"KraftDiner" <[EMAIL PROTECTED]> writes: > I have a dictionary and sometime the lookup fails... > it seems to raise an exception when this happens. > What should I do to fix/catch this problem? > > desc = self.numericDict[k][2] > KeyError: 589824 < This is the error that is being produc

Re: key not found in dictionary

2006-08-22 Thread Pierre Quentel
Depending on what you want to do if the key doesn't exist, you might want to use the dictionary method get() : value = some_dict.get(key,default) sets value to some_dict[key] if the key exists, and to default otherwise Regards, Pierre -- http://mail.python.org/mailman/listinfo/python-list

Re: key not found in dictionary

2006-08-22 Thread Philippe Martin
KraftDiner wrote: > I have a dictionary and sometime the lookup fails... > it seems to raise an exception when this happens. > What should I do to fix/catch this problem? > > desc = self.numericDict[k][2] > KeyError: 589824 < This is the error that is being produced, > because there is

Re: key not found in dictionary

2006-08-22 Thread Chaz Ginger
KraftDiner wrote: > I have a dictionary and sometime the lookup fails... > it seems to raise an exception when this happens. > What should I do to fix/catch this problem? > > desc = self.numericDict[k][2] > KeyError: 589824 < This is the error that is being produced, > because there is n

Re: key not found in dictionary

2006-08-22 Thread [EMAIL PROTECTED]
KraftDiner wrote: > I have a dictionary and sometime the lookup fails... > it seems to raise an exception when this happens. > What should I do to fix/catch this problem? > > desc = self.numericDict[k][2] > KeyError: 589824 < This is the error that is being produced, > because there is no

Re: key not found in dictionary

2006-08-22 Thread hiaips
KraftDiner wrote: > I have a dictionary and sometime the lookup fails... > it seems to raise an exception when this happens. > What should I do to fix/catch this problem? > > desc = self.numericDict[k][2] > KeyError: 589824 < This is the error that is being produced, > because there is n

key not found in dictionary

2006-08-22 Thread KraftDiner
I have a dictionary and sometime the lookup fails... it seems to raise an exception when this happens. What should I do to fix/catch this problem? desc = self.numericDict[k][2] KeyError: 589824 < This is the error that is being produced, because there is no key 589824. -- http://mail.p