[EMAIL PROTECTED] wrote:
> Problem:
> 
> You have a list of unknown length, such as this: list =
> [X,X,X,O,O,O,O].  You want to extract all and only the X's.  You know
> the X's are all up front and you know that the item after the last X is
> an O, or that the list ends with an X.  There are never O's between
> X's.
> 
> I have been using something like this:
> while list[0] != O:
>     storage.append(list[0])
>     list.pop(0)
>     if len(list) == 0:
>         break
> But this seems ugly to me, and using "while" give me the heebies.  Is
> there a better approach?
> 

Your names could be better as someone mentioned.
     ex, oh = 7, 13   # for example
     data = [ex, ex, ex, oh, oh, oh, oh]
If you need a list distinct from the original:
     try:
         result = data[: data.index(oh)]
     except ValueError:
         result = list(data)

Or you could simply:
     try:
         data = data[: data.index(oh)]
     except ValueError:
         pass
and data will be either the sublist you want or the original list.

-- 
-Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to