Which is the best way...

Bjorn Pettersen BPettersen at NAREX.com
Wed Aug 15 17:07:18 EDT 2001


> From: Matthew D. Wood [mailto:woodm at equire.com]
> 
> I have an interesting code-correctness question.  I firmly believe in 
> doing things 'The Right Way' and I'm not sure which of these 
> 3 blocks is 
> better.  So, if you code-guru's could help me out I would definitely 
> appreciate it.
> 
[snip]
> 
> # Works, but how much overhead is there in the slicing of the 
> range list?
> def first_non_ordered_2 (my_list) :
>        for index in range(len(my_list))[:-1] :
>                if my_list[index] > my_list[index + 1] :
>                        return index + 1
> 
>        else :
>                return None

you could always do range( len(my_list)-1 ), and you don't really need
the else:

 def first_non_ordered_2 (my_list) :
     for index in range( len(my_list)-1 ):
         if my_list[index] > my_list[index + 1] :
              return index + 1
     return None

-- bjorn




More information about the Python-list mailing list