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
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
> 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
"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
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
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
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
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
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
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
10 matches
Mail list logo