How to generate the power set of three items in grasshopper?

I have a list of three items, A, B, C.
I want the power set: {}, {A}, {B}, {C}, {AB}, {BC}, {CA}, {ABC}. (I don’t really care about the empty set).
I’ve tried multiple series components, but am getting confused. Maybe there is already a dedicated component for this I’m missing?

I’d do something like this:

SuperSet.gh (11.6 KB)

[edit, forgot one :P]

That’s perfect, thank you!

Now I just have to figure out how to extend it to larger sets…

this literally blew my mind, source: algorithm - How to generate a power set of a given set? - Stack Overflow

using bin representation as culling pattern, just awesome


To build power set, following thing can be used:

  • Create a loop, which iterates all integers from 0 till (2^N)-1
  • Proceed to binary representation for each integer
  • Each binary representation is a set of N bits (for lesser numbers, add leading zeros). Each bit corresponds, if the certain set member is included in current subset.

Example, 3 numbers: a, b, c

number binary  subset
0      000      {}
1      001      {c}
2      010      {b}
3      011      {b,c}
4      100      {a}
5      101      {a,c}
6      110      {a,b}
7      111      {a,b,c}

didn’t know how to bin numbers without code .___. so that is what the Python does

PowerSet_inno.gh (13.1 KB)

I saw that Stack Overflow topic, but couldn’t do the implementation–thanks!

1 Like