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
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
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
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
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
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
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
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
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