key not found in dictionary

hiaips rosedb0 at gmail.com
Tue Aug 22 14:59:08 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.

It raises a KeyError, as you are seeing. Just use a try/except
construction and handle the error as required by your application:

try:
    desc = self.numericDict[k][2]
except KeyError, ke:
    <do something - report the error to the user? ignore the error?>




More information about the Python-list mailing list