Re: Dictionaries and incrementing keys

2011-06-25 Thread Raymond Hettinger
On Jun 14, 12:57 pm, Steve Crook wrote: > 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. It is very pythonesque in the it was the traditional one way to do it (also one of the

Re: Dictionaries and incrementing keys

2011-06-14 Thread Asen Bozhilov
Steve Crook wrote: > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. Instead of calling function you could use: d = {} d[key] = (key in d and d[key]) + 1 Regards. -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steven D'Aprano
On Tue, 14 Jun 2011 12:53:11 +, Steve Crook wrote: > On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: > <078c5e9a-8fad-4d4c-b081-f69d0f575...@v11g2000prk.googlegroups.com>: > >> How do those methods compare to the one I normally use; >> >> try: >> dict[key]+=1 >> exce

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steven D'Aprano
On Tue, 14 Jun 2011 10:57:44 +, Steve Crook wrote: > 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 comp

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steve Crook
On Tue, 14 Jun 2011 13:16:47 +0200, Peter Otten wrote in Message-Id: : > Your way is usually faster than > >> dict[key] = dict.get(key, 0) + 1 Thanks Peter, ran it through Timeit and you're right. It's probably also easier to read the conditional version, even if it is longer. > You may also c

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steve Crook
On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: <078c5e9a-8fad-4d4c-b081-f69d0f575...@v11g2000prk.googlegroups.com>: > How do those methods compare to the one I normally use; > > try: > dict[key]+=1 > except: > dict[key]=1 This is a lot slower in percentage terms. You

Re: Dictionaries and incrementing keys

2011-06-14 Thread AlienBaby
On Jun 14, 12:16 pm, Peter Otten <__pete...@web.de> wrote: > Steve Crook wrote: > > I've always done key creation/incrementation using: > > > if key in dict: > >     dict[key] += 1 > > else: > >     dict[key] = 1 > > Your way is usually faster than > > > dict[key] = dict.get(key, 0) + 1 > > > Whils

Re: Dictionaries and incrementing keys

2011-06-14 Thread Peter Otten
Steve Crook wrote: > I've always done key creation/incrementation using: > > if key in dict: > dict[key] += 1 > else: > dict[key] = 1 Your way is usually faster than > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more compact, I'd be interested in views on how > pythonesque t

Dictionaries and incrementing keys

2011-06-14 Thread Steve Crook
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. -- http://mail.p