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

688 views Asked by At

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.

2

There are 2 answers

2
Mayank Porwal On BEST ANSWER

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']
1
ombk On

Basic solution

list_turn = []
a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]
for i in range(len(a)):
    list_turn.append(a[i])
    list_turn.append(b[i])