Combination list python
Question: I want to make a list of all posible combinations of a list

example:

list = [1, 2, 3, 4]

and the output should give me: [[1], [2], [3], [4], [1, 2] ... [1, 2, 3, 4]]

(I have to look for a combination which sum gives me the highest possible
value which is less than or equal to variable B, in this case if B = 9 the
output that I would like to get is [2, 3, 4])

My current solution is to use loops which have a gives a value of either 0 or
1, when value == 1 the number is added to a list. The problem with this is
that the maximum amount of nested loops is lower then the amount of items in
my list, it is rather slow and this is absolutely not an elegant solution.

Can somebody help me?


Answer: 
    from itertools import combinations,chain
    
    l= [1, 2, 3, 4]
    print(list(chain.from_iterable(map(list,combinations(l,i)) for i in range(1,len(l)+1))))
    [[1], [2], [3], [4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
    

