Python Forum
Combinations of list of lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Combinations of list of lists (/thread-10135.html)



Combinations of list of lists - dannyH - May-14-2018

I have a list of 25 lists of integers:
L = [[1,2,3], [4,5,6], [7,8],[9], …]


I want to create all possible combinations using all the integers from each list:

answer = [[1,4,7,9,...], [2,4,7,9,...], [3,4,7,9,...], [1,5,7,9,...], [2,5,7,9], …]


I tried:
for a in L[0]:
    for b in L[1]:
        for c in L[2]:
            ...
                answer = [a,b,c,d, …]
This works until the line length in IDLE exceeds about 90 characters, then the program refuses to run. Is there any way of using iterators, or any other solution?


RE: Combinations of list of lists - buran - May-14-2018

check itertools.product


RE: Combinations of list of lists - dannyH - May-14-2018

Thanks Buran.

I'm new to itertools, but I'll have a look tomorrow.

Danny

I've just found a thread from heiner55 which solves my problem in 1 line:

https://python-forum.io/Thread-How-to-find-the-number-of-permutations-of-a-list-of-lists

Many thanks for all the help from the forum.

Danny