2

I have this kind of list:

[['I'],['want','love','like'],['cat',dog]]

and I want to create a function that can return all combinations of words in the lists like this:

[['I'],['want'],['cat']]
[['I'],['love'],['cat']]
[['I'],['like'],['cat']]
[['I'],['want'],['dog']]
[['I'],['love'],['dog']]
[['I'],['like'],['dog']]

PS: The function must work with any n words

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
  • 1
    You can refer to this: https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists/33425736 – Himanshu Gupta Jun 21 '18 at 04:53

1 Answers1

5
import itertools
for k in itertools.product(*lst):
    print(k)
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45