Menu

gitpiper

powerset python Code Snippet in 2024

mathlistadvanced

Last Updated: 16 December 2024

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)]

python snippet similar to powerset For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️