a dict trick

Alex Popescu nospam.themindstorm at gmail.com
Thu Aug 2 04:10:49 EDT 2007


james_027 <cai.haibin at gmail.com> wrote in news:1186036331.304916.304020
@e9g2000prf.googlegroups.com:

> hi
> 
> for example I have this dictionary
> 
> dict = {'name':'james', 'language':'english'}
> 
> value = 'sex' in dict and dict['sex'] or 'unknown'
> 
> is a right pythonic of doing this one? I am trying to get a value from
> the dict, but if the key doesn't exist I will provide one.
> 
> THanks
> james
> 

Same question discussed at large not far ago:
subject: "Pythonic way of missing dict keys" [1]

bests,
./alex
--
.w( the_mindstorm )p.

[1] 
http://groups.google.com/group/comp.lang.python/browse_thread/thread/175eb4
5909a05a18




More information about the Python-list mailing list