'Any short way to create a combinations of arrays with fixed length and sum?

For example:

array = [4,0,0]

The generated arrays have to be of a fixed length and sum of the array has to be also fixed. What would be the shortest and efficient way to get this output (imported functions are allowed):

Output:

[4,0,0]
[0,4,0]
[0,0,4]
[3,1,0]
[0,3,1]
[1,0,3]
[1,3,0]
[0,1,3]
[3,0,1]
[2,1,1]...


Solution 1:[1]

The following solution is not exactly what you are looking for, i.e., it does not output in the same order as you have mentioned, but the values returned inside the list are the same.

from itertools import product
  
def findPairs(sum_value, len_value):
    lst = range(sum_value + 1)

    return [
        pair 
        for pair in product(lst, repeat=len_value) 
        if sum(pair) == sum_value
    ]

# ----------------------------------------- #

array = [4, 0, 0]
sum_value = sum(array)
len_value = len(array)

print(findPairs(sum_value, len_value))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1