[Tutor] simple list query

Alan Gauld alan.gauld at freenet.co.uk
Mon Jan 3 01:50:06 CET 2005


> I have a list consisting of about 250 items, I need to know if a
> particular item is in the list. I know this is better suited to  a
> dictionary but thats not the way it ended up ;-)
>
> I could do a for loop to scan the list & compare each one, but I
have a
> suspission that there is a better way ?

In principle you are right but due to the way Python works you
might be faster doing a sort on the list first, then you only
need to search until you pass the place where the entry should
be... This will be faster if you have to search for several
items, and might be faster even for one since the sort is
written in C but the for loop will be in Python...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list