[Tutor] Simple way to compare Two lists

Tom Fitzhenry tomfitzyuk at gmail.com
Fri Aug 10 11:50:43 CEST 2007


On Fri, Aug 10, 2007 at 02:54:44AM -0700, Jaggo wrote:
> Can anyone think of any better way?

If SmallList and BigList are sorted (in order), there is a faster method:

def IsAPartOfList(SmallList,BigList):
    for i in BigList:
        for j in SmallList:
            if i==j:
                return true
            if i>j:
                break
    return false

(I'm not sure how encouraged using break statements are, so wait for a tutor to
answer)

If one list is already sorted but the other isn't, it may still be faster to
sort the unsorted list then use the method above.

If neither SmallList or BigList are sorted, it's probably best to use your
original method, which I cannot improve.

-- 
Tom Fitzhenry



More information about the Tutor mailing list