[Tutor] quick speed question

Evert Rol evert.rol at gmail.com
Thu Sep 16 09:16:41 CEST 2010


> 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': '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, but it's still unsafe practice).

Cheers,

  Evert

> 
> Thanks,
> 
> T
> -- 
> C.T. Matsumoto
> Claes de Vrieselaan 60a III
> 3021 JR Rotterdam
> The Netherlands
> 
> tel.: +31 (0)6 41 45 08 54
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list