0

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.

  • See also [1](https://stackoverflow.com/questions/28395945/how-to-concatenate-two-lists-so-that-elements-are-in-alternative-position), [2](https://stackoverflow.com/questions/3471999/how-do-i-merge-two-lists-into-a-single-list), [3](https://stackoverflow.com/questions/11125212/interleaving-lists-in-python) and [4](https://stackoverflow.com/questions/7946798/interleave-multiple-lists-of-the-same-length-in-python). In the future, please use the _search_ tool to avoid duplicates and please show a code attempt if it is a fresh question. Thanks. – ggorlen Nov 25 '20 at 15:59

2 Answers2

3

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']
Mayank Porwal
  • 27,201
  • 7
  • 25
  • 45
  • 1
    It worked, thank you! – Emile Everduim Nov 25 '20 at 15:58
  • with no itertools: t = [x for x in zip(a,b)] and then flatten like in https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists flat_list = [] for sublist in t: for item in sublist: flat_list.append(item) – Docuemada Nov 25 '20 at 16:04
0

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])
ombk
  • 1,900
  • 1
  • 2
  • 15
  • Not sure who downvoted this, i know the loop is too specific, but that's a basic valid solution ... – ombk Nov 25 '20 at 15:57