[Tutor] How to control the not existent keys in a dict

Emilio Casbas ecasbasj at yahoo.es
Mon May 11 23:47:03 CEST 2009


Thanks to all for the responses.
Problem solved!, and I get a lot of valuable info.

Thanks
Emilio




----- Mensaje original ----
> De: Alan Gauld <alan.gauld at btinternet.com>
> Para: tutor at python.org
> Enviado: lunes, 11 de mayo, 2009 1:59:32
> Asunto: Re: [Tutor] How to control the not existent keys in a dict
> 
> 
> "Emilio Casbas" wrote
> 
> > If I enter a not existent key, an exception is raised.
> > How can I control the not existent key and assign him a default value?
> 
> One way is to use the get() method of a dictionary:
> 
> >>> d = {'Exists': True}
> 
> >>> d['Exists']
> True
> >>> d['Error']
> Traceback!!!....
> >>> d.get('Error',False)
> False
> 
> HTH,
> 
> 
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



      


More information about the Tutor mailing list