Skip to content

Instantly share code, notes, and snippets.

@codemaniac
Created June 27, 2012 17:22
Show Gist options
  • Save codemaniac/3005522 to your computer and use it in GitHub Desktop.
Save codemaniac/3005522 to your computer and use it in GitHub Desktop.
Powerset in python using list comprehensions
#!/usr/bin/python
# -*- coding: utf-8 -*-
def powerset(s):
return [[s[j] for j in xrange(len(s)) if (i&(1<<j))] for i in xrange(1<<len(s))]
s = ['a','b','c','d']
print powerset(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment