[Tutor] Joining multiple lists

alan.gauld@bt.com alan.gauld@bt.com
Thu, 24 May 2001 11:07:11 +0100


> a = 1,2,3
> b = 5,10,15
> c = 20,30,40

These aren't lists but if you enclose them in [] they 
would be. In which case:

new = a+b+c

should do the trick.

> 
> The results would be something like:
> 
> ((1,5,20), (1,5,30), (1,5,40), (1,10,20),
> (1,10,30),(1,10,40),(1,15,20),(1,15,30)....etc).

Ah, that's different, you want all the permutations.

I think for that you need nested for loops, 
something like:

result = []
for indexA in len(a):
   for indexB in len(b):
      for indexC in len(c):
         result.append((a[indexA],b[indexB],c[index[C]))

Maybe the new listcomprehensions can do it more easily 
- I think they can but haven't played withb them enough...   

Alan G.