Python Forum

Full Version: Combinations of list of lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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-fi...t-of-lists

Many thanks for all the help from the forum.

Danny