469,588 Members | 2,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

a dict trick

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

Aug 2 '07 #1
8 1910
On Thu, 02 Aug 2007 06:32:11 +0000, james_027 wrote:
hi

for example I have this dictionary

dict = {'name':'james', 'language':'english'}
First of all, this is a bad name because it shadows (overwrites) the
reference to the builtin constructor, `dict`.
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.
If you're using using Python 2.5, you could do this without `and`/`or`
trickery::
>>d['name'] if 'name' in d else 'unknown'
'james'
>>d['sex'] if 'sex' in d else 'unknown'
'unknown'

But there are more elegant ways. For example, the `get` method::
>>d.get('name', 'unknown')
'james'
>>d.get('sex', 'unknown')
'unknown'

See the `Python Library Reference, 3.8: Mapping types <http://
docs.python.org/lib/typesmapping.html#l2h-294>` for more information on
`dict` methods.

Or you could use the `collections.defaultdict <http://docs.python.org/lib/
defaultdict-objects.html>` type (new in 2.5, too), which I consider most
elegant::
>>from collections import defaultdict
d2 = defaultdict(lambda:'unknown', d)
d2['name']
'james'
>>d2['sex']
'unknown'

HTH,
Stargaming

Aug 2 '07 #2
james_027 wrote:
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

This fails if 'sex' is in the dictionary and it's value happens to be
any of the values that evaluate to a boolean False. In that case you'll
get 'unknow' even though the key is in the dictionary.

However, python dictionaries provide a way to do this:

dict.get('sex', 'unknown')

looks up the value associated with the key if it exists, otherwise it
returns the provided default value.

You may also want to checkout the dictionary method setdefault which has
the functionality of get PLUS if the key is not found, it adds the
key,value pair to the dictionary.

Gary Herron

Aug 2 '07 #3
james_027 wrote:
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
value = your_dict.get(key, default_value)
Aug 2 '07 #4
james_027 <ca********@gmail.comwrote 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..../thread/175eb4
5909a05a18

Aug 2 '07 #5
Hi,

what if we're not dealing with dict? is there a pythonic way of doing
ternary? the bool ? x:y

Thanks
james

Aug 2 '07 #6
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')

Aug 2 '07 #7
james_027 a écrit :
Hi,

what if we're not dealing with dict? is there a pythonic way of doing
ternary? the bool ? x:y
Python 2.5 introduced the following syntax:

expr1 if condition else expr2

In older Python versions, one has to use and/or (like you wrongly did)
or tuple/dict dispatch or other ad-hoc tricks. Or use a plain old
if/else branch.
Aug 2 '07 #8
In article <11**********************@e9g2000prf.googlegroups. com>,
james_027 <ca********@gmail.comwrote:
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.
Hi, James,

You might prefer:

d = {'name': 'James', 'language': 'English'}

value = d.get('sex', 'unknown')

This accomplishes what your above code does, using a method of the
built-in dict object.

If you also wish to ADD the new value to the dictionary, you may also
use the following:

value = d.setdefault('sex', 'unknown')

This returns the same value as the above, but also adds the key 'sex' to
the dictionary as a side-effect, with value 'unknown'.

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Aug 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Robin Cull | last post: by
17 posts views Thread by Pierre Fortin | last post: by
1 post views Thread by Alexander Kervero | last post: by
15 posts views Thread by George Sakkis | last post: by
12 posts views Thread by jeremito | last post: by
4 posts views Thread by Mike P | last post: by
12 posts views Thread by Florian Brucker | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.