Generate unique permutations of set and subsets

I’d like to generate permutations of an iterable, including those for subsets of the iterable, without duplicates or the empty set:

(1,2,3)  -->  (
    (1,),
    (2,),
    (3,),
    (1, 2),
    (1, 3),
    (2, 3),
    (1, 2, 3),
    (1, 3, 2),
    (2, 1, 3),
    (2, 3, 1),
    (3, 1, 2),
    (3, 2, 1)
)

Answer

This is essentially the powerset function provided by more-itertools combined with itertools.permutations:

from itertools import combinations, permutations

def powerset_permutations(iterable):
    "([1,2]) --> (1,) (2,) (1,2) (2,1)"
    s = list(iterable)
    for r in range(1, len(s)):
        yield from combinations(s, r)
    yield from permutations(s)
>>> list(powerset_permutations([1, 2, 3]))
[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

To include the empty set, change range(1, len(s)) to range(len(s)):

def powerset_permutations(iterable):
    "([1,2]) --> (1,) (2,) (1,2) (2,1)"
    s = list(iterable)
    for r in range(len(s)):
        yield from combinations(s, r)
    yield from permutations(s)
>>> list(powerset_permutations([1,2,3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]