Define a function takewhile(alive, seq) that takes elements from the front of the sequence seq as long as the predicate function alive returns True. The function should return a new list containing the taken elements.

Also define the dropwhile(alive, seq) variant that drops elements from the front of the sequence as long as alive returns True, returning the remaining elements as a new list.

def takewhile(alive, seq):
        """Take elements from seq while alive returns True.

        >>> takewhile(lambda x: x < 3, [1, 2, 3, 4, 1])
        [1, 2]
        >>> takewhile(lambda x: x != 0, [1, 2, 3])
        [1, 2, 3]
        >>> takewhile(lambda x: x > 0, [-1, 2, 3])
        []
        >>> takewhile(lambda x: True, [1, 2, 3])
        [1, 2, 3]
        """
Library
itertools.takewhile

Updated: