Harshavardhan Reddy Harshavardhan Reddy -4 years ago 104
Python Question

Permutations of list of list of lists

I want to find the permutation of list of list of lists
For example:
my input

x = [[[1,2,3],[5,6,7]],[[8,9,10],[11,12]]]


Required output should be:

[[[1,2,3],[8,9,10]],[[1,2,3],[11,12]],[[5,6,7],[8,9,10]],[[5,6,7],[11,12]]]


As you can see I want the innermost list to be intact and need to have that considered as an element and then do combinations.

I tried permutations(array) in itertools. But it didn't work.

Any help is highly appreciated.

Thank you.

Answer Source

You need itertools.product

import itertools

x = [[[1,2,3],[5,6,7]],[[8,9,10],[11,12]]]
for combo in itertools.product(*x):
    print combo

Output:

([1, 2, 3], [8, 9, 10])
([1, 2, 3], [11, 12])
([5, 6, 7], [8, 9, 10])
([5, 6, 7], [11, 12])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download