Skip to content

Instantly share code, notes, and snippets.

@eugenius1
Last active January 24, 2017 23:37
Show Gist options
  • Save eugenius1/9281dd50c19fecc6e50e7335a169295a to your computer and use it in GitHub Desktop.
Save eugenius1/9281dd50c19fecc6e50e7335a169295a to your computer and use it in GitHub Desktop.
All subsets of a list
def all_subsets(arr):
"""Returns a list of all subsets of a given list.
Mathematically: a set of all subsets of a set
Notes:
- Duplicates are treated distinct, i.e no duplicate checking or filtering.
- Recursive implementation.
"""
set_length = len(arr)
if set_length == 0:
return [[]]
else:
subsets = all_subsets(arr[1:])
return subsets + [sub + [arr[0]] for sub in subsets]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment