Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

merging two lists in python

Python Program to get two lists and merge them into single list.

Sample Input 1:

3 5 4 2 4 9 7 6 3

Sample Output 1:

5 4 2 9 7 6 3

Program or Solution

					    
							
l1=list(map(int,input("Enter Numbers:").split()))
l2=list(map(int,input("Enter Numbers:").split()))
l3=[None]* (len(l1)+len(l2))
k=0
for i in range(0,len(l1)):
    l3[k]=l1[i]
    k+=1

for i in range(0,len(l2)):
    l3[k]=l2[i]
    k+=1

print(l3)

					    
					

Program Explanation

Create a new list with size of size of a list + size of b list. l3 = len(l1) + len(l2) visit elements of list l1 and store it in l3. l3[k]=l1[i] visit elements of list l2 and store it in l3. l3[k]=l2[i]