Sum of lists for each element of list1 with all in list2
Question: I want make script that reads lines from file, than takes slices from each
line, combines all slices from 1 line with all slices from 2 line, then
combines all slices from previous step with 3rd line.

For example, we have

    
    
    Stackoverflow (4)
    python (3)
    question (3)
    

I get first list with slices of (number) letters.

    
    
    lst = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']
    

Then i need to combine it with second list:

    
    
    lst = ['pyt', 'yth', 'tho', 'hon']
    

Desired output:

    
    
    finallist = ['Stacpyt', 'tackpyt', 'ackopyt', 'ckovpyt', 'kovepyt', 'overpyt', 'verfpyt', 'erflpyt', 'rflopyt', 'flowpyt' 'Stacyth', 'tackyth', 'ackoyth', 'ckovyth', 'koveyth', 'overyth', 'verfyth', 'erflyth', 'rfloyth', 'flowyth', ..... ,  'erflhon', 'rflohon', 'flowhon']
    

then with 3rd list:

    
    
    lst = ['que', 'ues', 'est', 'sti', 'tio', 'ion']
    
    finallist = ['Stacpytque', 'tackpytque', 'ackopytque', 'ckovpytque', 'kovepytque', 'overpytque', 'verfpytque', 'erflpytque', 'rflopytque', .... 'erflhonion', 'rflohonion', 'flowhonion']
    

I stuck at point where I need to make finallist with combined results.

I am trying pieces of code like this, but its wrong:

    
    
    for i in lst:
        for y in finallist:
            finallist.append(i + y)
    

So if finallist is empty - it should copy lst in first loop iteration, and if
finallist is not empty it should combine each element with lst and so on.


Answer: Use [**ittertools**](https://docs.python.org/2/library/itertools.html
"itertools — Functions creating iterators for efficient looping")

    
    
    import itertools
    list1 = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']
    list2 = ['pyt', 'yth', 'tho', 'hon']
    list3 = ['que', 'ues', 'est', 'sti', 'tio', 'ion']
    
    final_list = list(itertools.product(list(itertools.product(list1,list2)),list3))
    

This will give you all combinations, then you can just join all of them to get
your string.

