Python yield multiple assignment
Question: I generally try to use yield whenever I can, but I don't get how I'd do it on
code like this:

    
    
    numbers = [1,2,3,4,5,6,7,8,9,10]
    
    def odd_and_even(numbers):
        odd = []
        even = []
        for number in numbers:
            if number % 2:
                odd.append(number)
            else:
                even.append(number)
        return (odd, even)
    

Obviously, in the above case, I could rewrite it as two different functions,
but for something more complex, that might be quite computationally expensive
because I'd be running twice as many checks.

How could the above code be made to use yield?


Answer: Using [`partition()`
function](http://nedbatchelder.com/blog/201306/filter_a_list_into_two_parts.html):

    
    
    import itertools
    
    def partition(items, predicate=bool):
        a, b = itertools.tee((predicate(item), item) for item in items)
        return ((item for pred, item in a if not pred),
                (item for pred, item in b if pred))
    
    odd, even = partition(numbers, lambda n: n % 2 == 0)
    

If `odd`, `even` are not consumed nearly in sync or if the predicate is not
expensive then your list version (from the question) should be faster.

