def join_on_key(key, a, b):
    """Join two iterables on a shared key.

    This will merge the object in a and the object in b that have
    the same value for key.  It will drop an unpaired objects.  It
    assumes the values for key are unique in each of a and b.
    For keys shared between a and b, the b value will be used.
    """
    a_store = {}
    b_store = {}
    a_iter = a.__iter__()
    b_iter = b.__iter__()
    a_active = True
    b_active = True
    while a_active or b_active:
        if a_active:
            try:
                x = next(a_iter)
                x = dict(x)
                if x[key] in b_store:
                    y = b_store.pop(x[key])
                    x.update(y)
                    yield x
                else:
                    a_store[x[key]] = x
            except StopIteration:
                a_active = False
        if b_active:
            try:
                y = next(b_iter)
                if y[key] in a_store:
                    x = a_store.pop(y[key])
                    x.update(y)
                    yield x
                else:
                    b_store[y[key]] = y
            except StopIteration:
                b_active = False
