key not found in dictionary

Philippe Martin pmartin at snakecard.com
Tue Aug 22 15:14:05 EDT 2006


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 key
> 589824.

If you agree that the key is not there, then just catch the exception
(try ... except)

Philippe




More information about the Python-list mailing list