hi,
I am trying to do some aombination study. I have saw some codes on permutations and combinations.
But for all those programs input is a single list. I am trying to make combination from 4 lists.
I have input 4 lists. Now I want to generate several combination lists. Example output is given below
Hope some one can help me to generate such output. Thanks in advance.
Input Lists:
list1 = [1,2]
list2 = [A,B,C]
list3 = [X,Y]
list4 = [M,N,O]
Output Combination lists:
[1,A,X,M]
[1,A,X,N]
[1,A,X,O]
[1,A,Y,M]
[1,A,Y,N]
[1,A,Y,O]
[1,B,X,M]
[1,B,X,N]
[1,B,X,O]
[1,B,Y,M]
[1,B,Y,N]
[1,B,Y,O]
[1,c,X,M]
[1,c,X,N]
[1,c,X,O]
[1,c,Y,M]
[1,c,Y,N]
[1,c,Y,O]

[2,A,X,M]
[2,A,X,N]
[2,A,X,O]
[2,A,Y,M]
[2,A,Y,N]
[2,A,Y,O]
[2,B,X,M]
[2,B,X,N]
[2,B,X,O]
[2,B,Y,M]
[2,B,Y,N]
[2,B,Y,O]
[2,c,X,M]
[2,c,X,N]
[2,c,X,O]
[2,c,Y,M]
[2,c,Y,N]
[2,c,Y,O]
Best regards,
Sriram

Recommended Answers

All 6 Replies

list1 = ['1','2']
list2 = ['A','B','C']
list3 = ['X','Y']
list4 = ['M','N','O']

resu = ( )

for i in list1:
    for j in list2:
        for k in list3:
            for l in list4:
                resu = resu + ([i,j,k,l],)

print resu

Here the results are stored into a tuple. You can do what you want with this tuple (print it in this example).

Dear Friend(GlueMan)
Thank you for the code. Its right. But what I am looking at is a general function, where the number of lists ( list1 thru listN ) may vary. This code works fine for 4 number of lists.
Thank you .

Regards,
Sriram

Friends I could find the recipe from active state
"Recipe 496807: list of all combination from multiple lists "
Thanks for author.

Best regards,
Sriram

Did you ever thought of doing your work yourself?
I could find the recipe from active state

Obviously not (something like "This is __not__ freeprograms.com" should be displayed prominently on the website). I try to follow the rule that when a question is asked with words, I respond with words. And similarly, code with code.

Gentlemen!!
I agree with your comments, but I don't want to put some weird code , which is not working for me.
I thank for the ideas and codes provided by some friends over here.
Its a general expectation from the beginners like me to get some ideas from experts like you

Sriram

Obviously not (something like "This is __not__ freeprograms.com" should be displayed prominently on the website). I try to follow the rule that when a question is asked with words, I respond with words. And similarly, code with code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.