Re: Append a new value to dict

2008-10-23 Thread bearophileHUGS
slais-www: > Slower than > ... Okay, I seen there's a little confusion, I try to say it more clearly. Generally this is the faster version (faster than the version with get), especially if you use Psyco: version 1) if 'B' in counter: counter['B'] += 1 else: counter['B'] = 1

Re: Append a new value to dict

2008-10-23 Thread slais-www
[EMAIL PROTECTED] wrote: Marc 'BlackJack' Rintsch: counter['B'] = counter.get('B', 0) + 1 If you benchmark it, you will find that using the get() method it's quite slower. Slower than if 'B' in counter: > counter['B'] += 1 > else: > counter['B'] = 1 ? It is not slower than default

Re: Append a new value to dict

2008-10-23 Thread [EMAIL PROTECTED]
Frank Niemeyer wrote: > >> However incrementing a non-existing key throws an exception. > > Right. And that's exactly what I would expect, according to the > "principle of least surprise" Python tries to obey. There's simply no > way to increment a non-existent value - not without performing s

Re: Append a new value to dict

2008-10-23 Thread bearophileHUGS
Marc 'BlackJack' Rintsch: > counter['B'] = counter.get('B', 0) + 1 If you benchmark it, you will find that using the get() method it's quite slower. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Append a new value to dict

2008-10-23 Thread Frank Niemeyer
[EMAIL PROTECTED] schrieb: > Frank Niemeyer: >> There's simply no >> way to increment a non-existent value - not without performing some >> obscure implict behind-the-scenes stuff. > > Like importing and using a defaultdict(int). There's nothing implicit in explicitly defining some default behavi

Re: Append a new value to dict

2008-10-22 Thread Marc 'BlackJack' Rintsch
On Wed, 22 Oct 2008 10:12:56 -0700, bearophileHUGS wrote: > Frank Niemeyer: >> There's simply no >> way to increment a non-existent value - not without performing some >> obscure implict behind-the-scenes stuff. > > Like importing and using a defaultdict(int). > > >> > So you >> > either have t

Re: Append a new value to dict

2008-10-22 Thread bearophileHUGS
Frank Niemeyer: > There's simply no > way to increment a non-existent value - not without performing some > obscure implict behind-the-scenes stuff. Like importing and using a defaultdict(int). > > So you > > either have to use a workaround: > > >  >>> try: > > ...   counter['B'] += 1 > > ... ex

Re: Append a new value to dict

2008-10-22 Thread Frank Niemeyer
> However incrementing a non-existing key throws an exception. Right. And that's exactly what I would expect, according to the "principle of least surprise" Python tries to obey. There's simply no way to increment a non-existent value - not without performing some obscure implict behind-the-scenes

Re: Append a new value to dict

2008-10-16 Thread Pat
paul wrote: Pat schrieb: I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.s

Re: Append a new value to dict

2008-10-16 Thread Boris Borcic
Kirk Strauser wrote: While we're on the subject, use keyword arguments to dict like: foo.update(dict(quux='blah', baz='bearophile', jdd='dict')) was *much* slower, at 11.8s. Presumably you would save half of that time by writing simply foo.update(quux='blah', baz='bearophile', jdd

Re: Append a new value to dict

2008-10-13 Thread pruebauno
On Oct 13, 9:41 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Mon, 13 Oct 2008 14:10:43 +0200, Mathias Frey wrote: > > However incrementing a non-existing key throws an exception. So you > > either have to use a workaround: > > > >>> try: > > ... counter['B'] += 1 > > ... except K

Re: Append a new value to dict

2008-10-13 Thread Kirk Strauser
At 2008-10-13T13:14:15Z, [EMAIL PROTECTED] writes: > jdd: >> foo = {'bar': 'baz'} >> foo.update({'quux': 'blah'}) > > That creates a new dict, to throw it away. Don't do that. I use that if I'm changing many values at once, eg: foo.update({ 'quux': 'blah', 'baz' : 'bearophile

Re: Append a new value to dict

2008-10-13 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : jdd: foo = {'bar': 'baz'} foo.update({'quux': 'blah'}) That creates a new dict, to throw it away. Just to make it clear for the easily confused ones (like me...): bearophile is talking about the dict passed as an argument to foo.update - not about the behaviour

Re: Append a new value to dict

2008-10-13 Thread Marc 'BlackJack' Rintsch
On Mon, 13 Oct 2008 14:10:43 +0200, Mathias Frey wrote: > However incrementing a non-existing key throws an exception. So you > either have to use a workaround: > > >>> try: > ... counter['B'] += 1 > ... except KeyError: > ... counter['B'] = 1 > > Since this looks ugly somebody invented the

Re: Append a new value to dict

2008-10-13 Thread bearophileHUGS
jdd: > foo = {'bar': 'baz'} > foo.update({'quux': 'blah'}) That creates a new dict, to throw it away. Don't do that. Use the standard and more readable syntax: > foo = {...} > foo['quux'] = 'blah' Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Append a new value to dict

2008-10-13 Thread Tim Chase
In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.setdefault( k, [] ).append( v ) Naturally, right after writing my post I found that there is an easier way: table[ k ] = v Just to be clear...these do two VERY different things: >>> v1=42

Re: Append a new value to dict

2008-10-13 Thread Mathias Frey
Pat wrote: I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.setdefault( k, [

Re: Append a new value to dict

2008-10-13 Thread paul
Pat schrieb: I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.setdefault( k,

Re: Append a new value to dict

2008-10-13 Thread jdd
On Oct 13, 7:21 am, Pat <[EMAIL PROTECTED]> wrote: > Is there a better/easier way to code this in Python than the > obtuse/arcane setdefault code? foo = {'bar': 'baz'} foo.update({'quux': 'blah'}) -- http://mail.python.org/mailman/listinfo/python-list

Re: Append a new value to dict

2008-10-13 Thread Pat
Pat wrote: I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.setdefault( k, [