<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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? > > hope this is clear. > thanks > Use itertools.
>>> import itertools >>> lst = "X,X,X,O,O,O,O,O,X,X,X,X,O,X".split(",") >>> [z for z in itertools.takewhile(lambda x:x=="X",lst)] ['X', 'X', 'X'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list