How do I write many nested for-loops in a few lines of Python code?

Viewed 277

What I have: I have the list List123=[-13,3,12,1] and the 2-by-4-matrix Matrix123=numpy.zeros((2,4), dtype=Decimal).

What I want: I want to change all entries of the matrix to any entry of the list and then print it to the terminal. There are 4^(2*4)=65536 possible combinations. I want to print every combination.

How I do it now: Here is the my current code:

List123=[-13,3,12,1]

Matrix123=numpy.zeros((2,4), dtype=Decimal)

k=0
for k in List123:
    Matrix123[0,0]=k
    for k in List123:
        Matrix123[0,1]=k
        for k in List123:
            Matrix123[0,2]=k
            for k in List123:
                Matrix123[0,3]=k
                for k in List123:
                    Matrix123[1,0]=k
                    for k in List123:
                        Matrix123[1,1]=k
                        for k in List123:
                            Matrix123[1,2]=k
                            for k in List123:
                                Matrix123[1,3]=k
                                print Matrix123
                                print " "

My question: What is a more compact way to write this in only a few lines of code? I need to do the same for a 23-by-27 Matrix. That would mean that I have to write code for 23*27=621 for-loops manually, if I don't find a more compact way.

3 Answers
Related