471,338 Members | 993 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Check for dict key existence, and modify it in one step.

Im using this construct a lot:

if dict.has_key(whatever):
dict[whatever] += delta
else:
dict[whatever] = 1

sometimes even nested:

if dict.has_key(whatever):
if dict[whatever].has_key(someother):
dict[whatever][someother] += delta
else:
dict[whatever][someother] = 1
else:
dict[whatever]={}
dict[whatever][someother] = 1

there must be a more compact, readable and less redundant way to do
this, no?

Thanks,

Rodrigo

Aug 28 '07 #1
8 3123
rodrigo a écrit :
Im using this construct a lot:

if dict.has_key(whatever):
<ot>
Avoid using builtin names as identifiers (it shadows the builtin name).
</ot>

Unless you're using an old Python version, you'd be better using
if whatever in my_dict:
# do something here
dict[whatever] += delta
else:
dict[whatever] = 1

sometimes even nested:

if dict.has_key(whatever):
if dict[whatever].has_key(someother):
dict[whatever][someother] += delta
else:
dict[whatever][someother] = 1
else:
dict[whatever]={}
dict[whatever][someother] = 1

there must be a more compact, readable and less redundant way to do
this, no?
There are other ways, yes. With Python <= 2.4.x, you can use
dict.setdefault, with Python 2.5.x you can use a defaultdict (cf
http://docs.python.org/whatsnew/modules.html).

HTH
Aug 28 '07 #2
On Tue, 2007-08-28 at 14:36 +0000, rodrigo wrote:
Im using this construct a lot:

if dict.has_key(whatever):
dict[whatever] += delta
else:
dict[whatever] = 1
In Python 2.5 there's a defaultdict class, otherwise you can subclass
dict like this:

class CountingDictionary(dict):
def increment(self, key, delta=1):
self[key] = self.get(key, 0) + delta

Hope this helps!

--
Evan Klitzke <ev**@yelp.com>

Aug 28 '07 #3
evan,

yes, it does help. Works like it should:

class CountingDictionary(dict):
def increment(self, key, delta=1):
self[key] = self.get(key, 0) + delta

d = CountingDictionary()
d.increment('cat')
d.increment('dog',72)
print d
>>{'dog': 72, 'cat': 1}
Thanks!

Aug 28 '07 #4
You're right of course, I was unclear. I wasn't using 'dict' to
override the dict clas, but just as a standin for the example (the
actual dictionary names are varied).

Thanks,

Rodriog

Aug 28 '07 #5
rodrigo <ro********@gmail.comwrites:
Im using this construct a lot:

if dict.has_key(whatever):
dict[whatever] += delta
else:
dict[whatever] = 1
I'd prefer:

foo.setdefault(whatever, 0)
foo[whatever] += delta
sometimes even nested:

if dict.has_key(whatever):
if dict[whatever].has_key(someother):
dict[whatever][someother] += delta
else:
dict[whatever][someother] = 1
else:
dict[whatever]={}
dict[whatever][someother] = 1
foo.setdefault(whatever, {})
foo[whatever].setdefault(someother, 0)
foo[whatever] += delta
there must be a more compact, readable and less redundant way to do
this, no?
Hope that helps.

--
\ "I took a course in speed waiting. Now I can wait an hour in |
`\ only ten minutes." -- Steven Wright |
_o__) |
Ben Finney
Aug 28 '07 #6
Ben Finney <bi****************@benfinney.id.auwrites:
foo.setdefault(whatever, {})
foo[whatever].setdefault(someother, 0)
foo[whatever] += delta
Should, of course, be:

foo.setdefault(whatever, {})
foo[whatever].setdefault(someother, 0)
foo[whatever][someother] += delta

--
\ "My house is made out of balsa wood, so when I want to scare |
`\ the neighborhood kids I lift it over my head and tell them to |
_o__) get out of my yard or I'll throw it at them." -- Steven Wright |
Ben Finney
Aug 28 '07 #7
On Aug 28, 1:13 pm, rodrigo <rodrigo...@gmail.comwrote:
evan,

yes, it does help. Works like it should:

class CountingDictionary(dict):
def increment(self, key, delta=1):
self[key] = self.get(key, 0) + delta

d = CountingDictionary()
d.increment('cat')
d.increment('dog',72)
print d
>{'dog': 72, 'cat': 1}

Thanks!
You responded to the answer that made the least use of already
existing recourses in python. Just letting you know.

Aug 29 '07 #8
rodrigo a écrit :
You're right of course, I was unclear. I wasn't using 'dict' to
override the dict clas, but just as a standin for the example (the
actual dictionary names are varied).
I guessed so, but Python's behaviour wrt/ naming can be somewhat
surprising for newcomers, and accidentally shadowing builtins (or not
builtins FWIW) names is a common pitfall. So better to warn newcomers
lurking here !-)
Aug 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard A. DeVenezia | last post: by
25 posts views Thread by Michele Petrazzo | last post: by
25 posts views Thread by pamelafluente | last post: by
16 posts views Thread by chosechu | last post: by
4 posts views Thread by bearophileHUGS | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.