I have a python list, similar to the following:
foo = [1, 2, 3, 4, 5]
I'd like to find a way to sum some of the list elements, provided the sum does not exceed a specific value, such as "7" in the list above. I want it to find all the combinations that do not exceed "7".
I need it to return a result that looks something like:
[1, 2, 3]
[1, 4]
[2, 4]
[1, 5]
I'm aware I can already sum a complete list using sum(foo), but I only want to sum part of the list. I'm not aware of a way to introduce an if sum(foo) < 7 argument that only sums some of the terms.
How would I do this? Should I be using itertools.combinations?