def extract(seq, f):
    """
    Extract items from a possibly nested sequence.

    All items of the possibly nested sequence ``seq`` for which the
    callback ``f`` returns ``True`` are returned in a list.
    """
    try:
        it = iter(seq)
    except TypeError:
        return []
    items = []
    for item in it:
        if f(item):
            items.append(item)
        else:
            items.extend(extract(item, f))
    return items