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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Feb 15 02:44:10 EST 2006


In <1139976842.179801.285400 at z14g2000cwz.googlegroups.com>, Farel wrote:

> Which is Faster in Python and Why?

``if not b in m`` looks at each element of `m` until it finds `b` in it
and stops then.  Assuming `b` is in `m`, otherwise all elements of `m` are
"touched".

``if m.count(b) > 0`` will always goes through all elements of `m` in the
`count()` method.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list