Skip to content

Instantly share code, notes, and snippets.

@jtreminio
Last active March 31, 2025 14:51
Show Gist options
  • Save jtreminio/209c790d37c9a54e5b0f264f3eb3d9cb to your computer and use it in GitHub Desktop.
Save jtreminio/209c790d37c9a54e5b0f264f3eb3d9cb to your computer and use it in GitHub Desktop.
def get_combinations(input: list) -> list:
"""
get_combinations() can be used to find all possible combinations of a
flat list of values.
"""
# start with a non-empty payload just to get the initial for loop going
combinations = [[]]
# iterate over every value once
for x in input:
curr = []
# take previous combos and add current value, generating a new combo
for combination in combinations:
curr += [combination + [x]]
combinations += curr
# trim out the initial payload so we only get non-empty results
return combinations[1:]
result = get_combinations([0, 3, 6, 9])
"""
result when sorted:
[
[0],
[0, 3],
[0, 3, 6],
[0, 3, 6, 9],
[0, 3, 9],
[0, 6],
[0, 6, 9],
[0, 9],
[3],
[3, 6],
[3, 6, 9],
[3, 9],
[6],
[6, 9],
[9]
]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment