[Tutor] Limitation of range() function in Walking problem

Alan Gauld alan.gauld at freenet.co.uk
Sat Sep 16 09:44:49 CEST 2006


> I loop through each list and check if list[0] is in
> range of previous list list[1] AND if it is less or
> greater than. If less, I retain the greater number and
> loop through. ...
> example are small, it worked. in the real world
> example , it did not work, because computer slowed
> down and eventually crashed.  The range(81393417) is
> really big. 

Rather than use 'in' just compare against the limits.

if num in range(min, max):

becomes

if  min < num < max:

The first version has to construct a list size max 
then it has to cycle through the list comparing 
every entry with num. Because its a contiguous 
range you only want to compare with the bottom 
and top, which is much faster.

HTH,

Alan G.


More information about the Tutor mailing list