Dremendo Tag Line

Enter 5 numbers in two lists and print common numbers of both lists using sets in Python

Sets - Question 2

In this question, we will see how to enter 5 numbers in two lists and print the common numbers which are present in both the lists using sets in Python programming. To know more about sets click on the sets lesson.

Q2) Write a program in Python to enter 5 numbers in two lists and print the common numbers which are present in both the lists using sets.

    Example:-
    List A = [1, 2, 3, 4, 5]
    List B = [1, 6, 2, 7, 3]
    Common numbers present in both lists are
    1 2 3

Program

a=list()
b=list()

print('Enter 5 numbers in List a')
for i in range(5):
    a.append(int(input()))

print('Enter 5 numbers in List b')
for i in range(5):
    b.append(int(input()))

s1=set(a)
s2=set(b)
s3=s1.intersection(s2)

print('Common numbers present in both lists are')
for n in s3:
    print(n,end=' ')

Output

Enter 5 numbers in List a
1
2
3
4
5
Enter 5 numbers in List b
1
6
2
7
3
Common numbers present in both lists are
1 2 3
video-poster