This can be well written using a recursive generator:
def sentences(lists, i=0, sentence=''): if i == len(lists): yield sentence else: for word in lists[i]: yield from sentences(lists, i + 1, sentence + word)
And then
lists = [['Sometimes'],[' '],['I'],[' '],['love','hate'],[' '],['pizza','coke']] for s in sentences(lists): print s
which prints
Sometimes I love pizza Sometimes I love coke Sometimes I hate pizza Sometimes I hate coke
(To get the actual list, list(sentences(lists)) .)
Paul draper
source share