How to use iter function to loop through a list until a certain match is found
Question: I frankly don't understand how to use the
[`iter(o[,sentinel])`](http://docs.python.org/2/library/functions.html#iter)
function and I'm trying to loop through `lst` _(a list)_ and print all the
values until `'kdawg'`

Code:

    
    
    lst = [2,3,4,'kdawg',5,6,'hey']
    
    class foo:
        def __str__(self):
            return str(lst)
    
    for i in iter(foo,'kdawg'):
        print i
    

I expect it to return:

    
    
    2
    3
    4
    

However it returns the whole list **continuously:**

    
    
    [2,3,4,'kdawg',5,6,'hey']
    

Why doesn't my `iter()` function work?


Answer: Use
[`itertools.takewhile()`](http://docs.python.org/2/library/itertools.html#itertools.takewhile):

    
    
    >>> from itertools import takewhile
    >>> 
    >>> l = [2,3,4,'kdawg',5,6,'hey']
    >>> 
    >>> for i in takewhile(lambda s: s != 'kdawg', l):
    ...     print i
    ... 
    2
    3
    4
    

