[Tutor] For - if - else loop; print selective output

eryksun eryksun at gmail.com
Thu Oct 25 22:16:31 CEST 2012

On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
<ramit.prasad at jpmorgan.com> wrote:
> Do you happen to know offhand if there is a difference between
> `in <list>` vs. `in <tuple>` vs. `in <set>`?

The "in" comparison (__contains__ method) is equivalent for list and
tuple. It has to search through the sequence item by item, which makes
it an O(n) operation. On the other hand, a set/dict uses the hash() of
an object to map it to a known location in a table (performance
degrades if there are many collisions). On average, you can check if a
set/dict contains an item in constant time, i.e. O(1). The amortized
worst case is O(n).

More information about the Tutor mailing list