-4

Is there a function that generates all the permutations of a list of lists, while keeping their orders?

For example:

func([[1, 2, 3], [10, 20, 30]])

produces:

[[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30], [3, 10], [3, 20], [3, 30]]
Guy
  • 155
  • 11

1 Answers1

0

There is a product function in itertools module which does exactly that :

from itertools import product

lst = [[1, 2, 3], [10, 20, 30]]
res = [list(i) for i in product(*lst)]
print(res)

Because you want the inside items to be list as well, I used list(i) to convert the tuple which product gives into list.

Output:

[[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30], [3, 10], [3, 20], [3, 30]]

S.B
  • 13,077
  • 10
  • 22
  • 49