Power Set Generation in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In mathematics, the power set of a set is the set of all subsets of the set, including the empty set and the set itself. In Python, we can create a function that returns the powerset of a given iterable.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13706{{"`Power Set Generation in Python`"}} python/variables_data_types -.-> lab-13706{{"`Power Set Generation in Python`"}} python/for_loops -.-> lab-13706{{"`Power Set Generation in Python`"}} python/lists -.-> lab-13706{{"`Power Set Generation in Python`"}} python/tuples -.-> lab-13706{{"`Power Set Generation in Python`"}} python/function_definition -.-> lab-13706{{"`Power Set Generation in Python`"}} python/importing_modules -.-> lab-13706{{"`Power Set Generation in Python`"}} python/using_packages -.-> lab-13706{{"`Power Set Generation in Python`"}} python/standard_libraries -.-> lab-13706{{"`Power Set Generation in Python`"}} python/data_collections -.-> lab-13706{{"`Power Set Generation in Python`"}} python/build_in_functions -.-> lab-13706{{"`Power Set Generation in Python`"}} end

Powerset

Write a Python function called powerset(iterable) that takes an iterable as an argument and returns the powerset of the iterable. The function should follow these steps:

  1. Convert the given value to a list.
  2. Use range() and itertools.combinations() to create a generator that returns all subsets.
  3. 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)]

Summary

In this challenge, you have learned how to create a Python function that returns the powerset of a given iterable. The function uses range() and itertools.combinations() to create a generator that returns all subsets, and itertools.chain.from_iterable() and list() to consume the generator and return a list.

Other Python Tutorials you may like