Skip to content

Instantly share code, notes, and snippets.

@shiva-karthick
Created December 3, 2021 10:55
Show Gist options
  • Save shiva-karthick/4ed6a2323613406f0ad2fc55c508f7ad to your computer and use it in GitHub Desktop.
Save shiva-karthick/4ed6a2323613406f0ad2fc55c508f7ad to your computer and use it in GitHub Desktop.
## How to get all possible combinations of a list’s elements?
def combs(a):
if len(a) == 0:
return [[]]
cs = []
for c in combs(a[1:]):
cs += [c, c+[a[0]]]
return cs
combs([1,2,3,4,5])
## [[], [1], [2], [2, 1], [3], [3, 1], [3, 2], ..., [5, 4, 3, 2, 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment