Thursday, 16 December 2021

Python-Slip 28-B-Write a Python program to accept two lists and merge the two lists into list of tuple.

 

Write a Python program to accept two lists and merge the two lists into list of tuple.

 Method 1:-

 list1 = [1, 2, 3]

list2 = ['a', 'b', 'c']

 new_list = [(list1[i], list2[i]) for i in range(0, len(list1))]

 print(new_list)

 Method 2:-

def merge(list1, list2):

merged_list = [(list1[i], list2[i]) for i in range(0, len(list1))]

return merged_list

 # Driver code

list1 = [1, 2, 3]

list2 = ['a', 'b', 'c']

print(merge(list1, list2))

Method 3:-

a=[]

c=[]

n1=int(input("Enter number of elements:"))

for i in range(1,n1+1):

b=int(input("Enter element:"))

a.append(b)


n2=int(input("Enter number of elements:"))

for i in range(1,n2+1):

d=int(input("Enter element:"))

c.append(d)


 new_list = [(a[i], c[i]) for i in range(0, len(a))]

print(new_list)