[Tutor] Need help combining elements of a list of lists

Jim jf_byrnes at comcast.net
Tue Jul 10 22:46:57 EDT 2018


Say I have a list like ltrs and I want to print out all the possible 3 
letter combinations. I want to combine letters from each inner list but 
not combine any letters within the inner list itself. So ACF and ADF 
would be ok but ABC would not.

I can lay it out manually and see the pattern, I cannot figure out how 
to do it programically. Just in case this looks like homework it is not. 
It's a small test case I devised to try to figure it out so I can apply 
it to a bigger real world problem I am working on.

ltrs = [['A', 'B'], ['C', 'D', 'E'], ['F', 'G', 'H', 'I']]

print(ltrs[0][0]+ltrs[1][0]+ltrs[2][0]) #ACF
print(ltrs[0][0]+ltrs[1][1]+ltrs[2][0]) #ADF
print(ltrs[0][0]+ltrs[1][2]+ltrs[2][0]) #AEF
print(ltrs[0][0]+ltrs[1][0]+ltrs[2][1]) #ACG
print(ltrs[0][0]+ltrs[1][1]+ltrs[2][1]) #ADG
print(ltrs[0][0]+ltrs[1][2]+ltrs[2][1]) #AEG
.
.
.
print(ltrs[0][1]+ltrs[1][2]+ltrs[2][3]) #BEI

thanks,  Jim



More information about the Tutor mailing list