# One-liner combinations (minus the import). from itertools import chain combinations = lambda a,k: [()] if k==0 else chain.from_iterable(\ map(lambda i:map(lambda c:tuple(a[i:i+1])+c,combinations(a[i+1:],k-1)),\ range(len(a)))) # .. n = 5 a = list(range(1, 1+n)) for k in range(1+n): print(f'{n} choose {k}') for x in combinations(a, k): print(x)
Standard input is empty
5 choose 0 () 5 choose 1 (1,) (2,) (3,) (4,) (5,) 5 choose 2 (1, 2) (1, 3) (1, 4) (1, 5) (2, 3) (2, 4) (2, 5) (3, 4) (3, 5) (4, 5) 5 choose 3 (1, 2, 3) (1, 2, 4) (1, 2, 5) (1, 3, 4) (1, 3, 5) (1, 4, 5) (2, 3, 4) (2, 3, 5) (2, 4, 5) (3, 4, 5) 5 choose 4 (1, 2, 3, 4) (1, 2, 3, 5) (1, 2, 4, 5) (1, 3, 4, 5) (2, 3, 4, 5) 5 choose 5 (1, 2, 3, 4, 5)