[Tutor] quick speed question

Eduardo Vieira eduardo.susan at gmail.com
Thu Sep 16 16:47:23 CEST 2010


On Thu, Sep 16, 2010 at 1:16 AM, Evert Rol <evert.rol at gmail.com> 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, but it's still unsafe practice).
>
If you want to find out if 'foo' is a key, you can do: 'foo' in d,
which will return False. I'm not sure, but if I recall this can even
be faster than using the if.

Regards,
Eduardo


More information about the Tutor mailing list