On Thu, Sep 16, 2010 at 1:16 AM, Evert Rol wrote:
> The latter: they are not the same:
>
d = {'key': 'food'}
d['key'] == 'foo'
> False
'foo' in d['key']
> True
>
>
> Btw, generally don't use a reserved Python word for a variable, such as dict
> in this case (I know it's an example
On 2:59 PM, C.T. Matsumoto wrote:
Hello Tutors,
I was just wondering if you have a dictionary key is it faster to do:
if dict['key'] == 'foo':
...
or is this faster:
if 'foo' in dict['key']:
...
Or is there any difference and I'm chasing ghosts?
Thanks,
T
dict is not a good name
This is faster:
if dict['key'] == 'foo':
pass
...But just slightly. (About 9% faster on my machine. This may differ
depending on the implementation, but I doubt it. See the 'timeit'
module.) Whether it's a big deal depends on what you're doing.
Accessing the dict - a million times - w
> Hello Tutors,
>
> I was just wondering if you have a dictionary key is it faster to do:
>
> if dict['key'] == 'foo':
>...
>
> or is this faster:
>
> if 'foo' in dict['key']:
>...
>
> Or is there any difference and I'm chasing ghosts?
The latter: they are not the same:
>>> d = {'key