Check for dict key existence, and modify it in one step.
- From: rodrigo <rodrigo.cr@xxxxxxxxx>
- Date: Tue, 28 Aug 2007 14:36:50 -0000
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
.
- Follow-Ups:
- Re: Check for dict key existence, and modify it in one step.
- From: Ben Finney
- Re: Check for dict key existence, and modify it in one step.
- From: Evan Klitzke
- Re: Check for dict key existence, and modify it in one step.
- From: Jason
- Re: Check for dict key existence, and modify it in one step.
- From: Bruno Desthuilliers
- Re: Check for dict key existence, and modify it in one step.
- Prev by Date: Re: wxpython:how to minimize to taskbar
- Next by Date: Re: wxpython:how to minimize to taskbar
- Previous by thread: wxpython:how to minimize to taskbar
- Next by thread: Re: Check for dict key existence, and modify it in one step.
- Index(es):