-7

I have two lists with elements and an empty list:

l1 = [1,2,3,4,5,6]
l2 = [7,8,9]
l3 = []

How can I add the elements of l1 and l2 into l3, like that:

l3 = [1,7,2,8,3,9,4,5,6]
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Khan Khan
  • 119
  • 9
  • It seems you want to add elements into the resulted list **alternately** from the first and the second list. See [that question](https://stackoverflow.com/questions/3678869/pythonic-way-to-combine-two-lists-in-an-alternating-fashion) about ways for doing that. – Tsyvarev Apr 20 '22 at 23:13

1 Answers1

1
l1 = [1,2,3,4,5,6]
l2 = [7,8,9]
l3 = []


for i in range(len(l1)):     #iterates through indices of l1
    l3.append(l1[i])         #adds elements of l1 to l3 for index currently in loop for
    if l2:                   #if l2 is still not empty...
        l3.append(l2.pop(0)) #removes first element from l2 and adds it to l3 

print(l3)                    #outputs [1, 7, 2, 8, 3, 9, 4, 5, 6]
ntt0
  • 21
  • 3