is there a better way?

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Fri Feb 10 12:59:35 EST 2006


markscala at gmail.com wrote:

> 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.

What not

for x in list:
  if x == O:
    break
  storage.append(x)

??

-- 
Jeremy Sanders
http://www.jeremysanders.net/



More information about the Python-list mailing list