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

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Oct 25 23:55:05 CEST 2012


eryksun wrote:
> 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).

Sorry, I should have stated that I meant with regards to the 
optimization that loads these as a literal constant.

Ramit Prasad



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list