By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

a dict trick

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.