pythonpython-3.xlistconcatenationturn-by-turn

How do I put two list together as one but TURN-BY-TURN in Python


I have this lists:

a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]

And I want to combine these to:

c = ["1 Monday", "2 Tuesday", "1 Wednesday", "2 Thursday", "1 Friday", "2 Saturday"] 

I want to do this turn by turn. So append first element of a and first element of b and then second element of a and second element of b etc.


Solution

  • You can use itertools with zip:

    In [3585]: import itertools
    
    In [3586]: list(itertools.chain(*zip(a,b)))
    Out[3586]: 
    ['1 Monday',
     '2 Tuesday',
     '1 Wednesday',
     '2 Thursday',
     '1 Friday',
     '2 Saturday']