is there a better way?

Dave Hansen iddw at hotmail.com
Fri Feb 10 19:54:16 EST 2006


On Sat, 11 Feb 2006 01:37:59 +0100 in comp.lang.python, Schüle Daniel
<uval at rz.uni-karlsruhe.de> wrote:

>Lonnie Princehouse wrote:
>> everybody is making this way more complicated than it needs to be.
>> 
>> storage = list[:list.index(O)]
>
>the question is whether the old list is needed in the future or not
>if not then it would be easer/mor efficient to use
>
>del lst[lst.index(0):]

And you're both forgetting the list can end with X.  the index method
raises a ValueError exception if the desired value is not found in the
list.  Assuming you want to keep the original list and create a new
list called storage, you could try

   if lst[-1] == X:
      storage = lst[:]
   else:
      storage = lst[:lst.index(O)]

or even

   try:
      storage = lst[:lst.index(O)]
   except ValueError:
      storage = lst[:]

(WARNING: untested!)

Regards,


   
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list