Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7600
| From | Steve Crook <steve@mixmin.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Dictionaries and incrementing keys |
| Date | 2011-06-14 10:57 +0000 |
| Organization | Mixmin |
| Message-ID | <slrnivefl8.47d.steve@news.mixmin.net> (permalink) |
Hi all,
I've always done key creation/incrementation using:
if key in dict:
dict[key] += 1
else:
dict[key] = 1
Today I spotted an alternative:
dict[key] = dict.get(key, 0) + 1
Whilst certainly more compact, I'd be interested in views on how
pythonesque this method is.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Dictionaries and incrementing keys Steve Crook <steve@mixmin.net> - 2011-06-14 10:57 +0000
Re: Dictionaries and incrementing keys Peter Otten <__peter__@web.de> - 2011-06-14 13:16 +0200
Re: Dictionaries and incrementing keys AlienBaby <matt.j.warren@gmail.com> - 2011-06-14 05:37 -0700
Re: Dictionaries and incrementing keys Steve Crook <steve@mixmin.net> - 2011-06-14 12:53 +0000
Re: Dictionaries and incrementing keys Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-14 13:26 +0000
Re: Dictionaries and incrementing keys Steve Crook <steve@mixmin.net> - 2011-06-14 12:57 +0000
Re: Dictionaries and incrementing keys Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-14 13:24 +0000
Re: Dictionaries and incrementing keys Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-14 09:30 -0700
Re: Dictionaries and incrementing keys Raymond Hettinger <python@rcn.com> - 2011-06-25 01:13 -0700
csiph-web