Dremendo Tag Line

Enter 5 numbers in set and list and add numbers from list to the set in Python

Sets - Question 1

In this question, we will see how to to enter 5 numbers in a set and in a list and add numbers from the list to the set which are not present in the set but present in the list in Python programming. To know more about sets click on the sets lesson.

Q1) Write a program in Python to enter 5 numbers in a set and in a list. Now add numbers from the list to the set which are not present in the set but present in the list.

    Example:-
    List A = [1, 2, 3, 4, 5]
    List B = [1, 6, 2, 7, 3]
    Set after adding numbers from the lists
    {1, 2, 3, 4, 5, 6, 7}

Program

a=set()
b=list()

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

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

# Adding numbers from the list to the set except duplicate numbers
print('Set after adding numbers from the lists')
a.update(b)
print(a)

Output

Enter 5 numbers in a Set
1
2
3
4
5
Enter 5 numbers in a List
1
6
2
7
3
Set after adding numbers from the lists
{1, 2, 3, 4, 5, 6, 7}
video-poster