Which is faster? (if not b in m) or (if m.count(b) > 0)

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Feb 15 23:26:35 EST 2006


Two additional things beyond the important lesson to always time things:

1. Be careful with operator precedence. In this case, people got lucky:

    not b in m

has the precedence:

    not (b in m)

2. Python has a "not in" operator:

    b not in m

which is the clearest and fastest way to test for non-membership in a
container.

Tim Delaney



More information about the Python-list mailing list