Skip to content

Instantly share code, notes, and snippets.

@brycepg
Last active October 5, 2016 03:55
Show Gist options
  • Save brycepg/f8366d86637a3ac2727d26d1a18736b2 to your computer and use it in GitHub Desktop.
Save brycepg/f8366d86637a3ac2727d26d1a18736b2 to your computer and use it in GitHub Desktop.
Generate List Combinations Using Bitwise Arithmetic
def combinate(list_):
"""Generate all combinations of a list(unordered)"""
combinations = []
for count in range(1, 2**len(list_)):
combinations.append(
[elem for cur_index, elem in enumerate(list_)
if (count >> cur_index) & 1]
)
return combinations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment