I'm trying to write some Python code to perform some computational group theory tasks, the problem is that I'm really a mathematician rather than a programmer so I was wondering if any of you real programmers could help me out.

I have n functions, f_1, ..., f_n, and m words, u_1, ..., u_m. I want to perform each function on each word, which is easy enough:

Code:

list1=[]
for i in range(m):
list1.append([])
for j in range(n):
list1.append(f[j](u[i]))

The problem is that now I want to perform f_j on each result in the 2D list1, and then again on these results, and so on an arbitrary number of times until I observe some property in the results and break out of the loops.

So each time I need to add another dimension to my list and hence, as far as I can see (which isn't, admittedly, very far), I'll need another for loop.

What's the best way of doing this without having to hand code a huge number of nested for loops?

Thanks in advance for any help!