Re: [Tutor] Fast way to access items in a dictionary

2009-06-18 Thread Alan Gauld
"Elisha Rosensweig" wrote if 'someKey' in dict.keys(): someData = dict['someKey'] is there a faster way to do this? Faster in terms of execution speed? Sure just miss out the test... accessing a key that does not exist will through an exception, which is just as tiresome... Tiresom

Re: [Tutor] Fast way to access items in a dictionary

2009-06-17 Thread Luke Paireepinart
Looks like a simple 'in' is faster both when it's there... >>> Timer("'D' in {'D':123}.keys()").timeit() 0.93669924584355613 >>> Timer("'D' in {'D':123}").timeit() 0.34678047105990117 ... and when it isn't... >>> Timer("'E' in {'D':123}.keys()").timeit() 0.99194670371434768 >>> Timer("'E'

Re: [Tutor] Fast way to access items in a dictionary

2009-06-17 Thread Emile van Sebille
On 6/17/2009 5:39 PM Elisha Rosensweig said... Hi, Till now, when I receive a dictionary that I'm not sure contains a certain key, I would use the following template to access a given key: if 'someKey' in dict.keys(): someData = dict['someKey'] is there a faster way to do this? Looks l

Re: [Tutor] Fast way to access items in a dictionary

2009-06-17 Thread Wayne
On Wed, Jun 17, 2009 at 7:39 PM, Elisha Rosensweig wrote: > Hi, > > Till now, when I receive a dictionary that I'm not sure contains a certain > key, I would use the following template to access a given key: > > if 'someKey' in dict.keys(): >someData = dict['someKey'] > > is there a faster way

[Tutor] Fast way to access items in a dictionary

2009-06-17 Thread Elisha Rosensweig
Hi, Till now, when I receive a dictionary that I'm not sure contains a certain key, I would use the following template to access a given key: if 'someKey' in dict.keys(): someData = dict['someKey'] is there a faster way to do this? accessing a key that does not exist will through an exception