# Powerset

Python, Math, List · Nov 2, 2020

Returns the powerset of a given iterable.

• Use `list()` to convert the given value to a list.
• Use `range()` and `itertools.combinations()` to create a generator that returns all subsets.
• Use `itertools.chain.from_iterable()` and `list()` to consume the generator and return a list.
```from itertools import chain, combinations

def powerset(iterable):
s = list(iterable)
return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))```
`powerset([1, 2]) # [(), (1,), (2,), (1, 2)]`

### Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

## More like this

• ### Arithmetic progression

Python, Math

Generates a list of numbers in the arithmetic progression starting with the given positive integer and up to the specified limit.

• ### Digitize number

Python, Math

Converts a number to a list of digits.

• ### Mapped list average

Python, Math

Calculates the average of a list, after mapping each element to a value using the provided function.