'Combining of two lists in python [closed]
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]
Solution 1:[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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |