a dict trick
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Aug 2 04:38:58 EDT 2007
james_027 a écrit :
> 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?
No. The first problem is that using 'dict' as an identifier, you're
shadowing the builtin dict type. The second problem is that you're
reinventing the square wheel.
> I am trying to get a value from
> the dict, but if the key doesn't exist I will provide one.
d = {'name':'james', 'language':'english'}
d.get('sex', 'unknown')
More information about the Python-list
mailing list