import itertools


def powerset(iterable):
    """Yield all subsets of given finite iterable.

    Based on code from itertools docs.

    Arguments:
    iterable -- finite iterable
      We yield all subsets of the set of all items yielded by iterable.

    >>> sorted(list(powerset([1,2,3])))
    [(), (1,), (1, 2), (1, 2, 3), (1, 3), (2,), (2, 3), (3,)]

    """
    s = list(iterable)
    return itertools.chain.from_iterable( itertools.combinations(s, r)
        for r in range(len(s)+1) )