Python program to find missing and additional values in two lists?

PythonProgrammingServer Side Programming

In set theory, the complement of a set A refers to elements not in A. the relative complement of A with respect to a set B, also termed the difference of Sets A and B. We just apply this principle here. Python has difference function.

Algorithm

Step 1 : first we create two user input list.
   A & B
Step 2 : Insert A and B to a set.
Step 3 : for finding the missing values of first list we apply difference function, difference of B from A.
Step 4 : for finding the Additional values of first list we apply difference function, difference of A from B.
Step 5 : Same procedure apply for Second list also.

Example Code

#To find the missing and additional elements 
A=list()
B=list()
n1=int(input("Enter the size of the First List ::"))
n2=int(input("Enter the size of the second List ::"))
print("Enter the Element of first List ::")
for i in range(int(n1)):
   k=int(input(""))
   A.append(k)
print("Enter the Element of second List ::")
for j in range(int(n2)):
   k1=int(input(""))
   B.append(k1)
# prints the missing and additional elements in first list
print("Missing values in first list:", (set(B).difference(A)))
print("Additional values in first list:", (set(A).difference(B)))

# prints the missing and additional elements in second list 
print("Missing values in second list:", (set(A).difference(B)))
print("Additional values in second list:", (set(B).difference(A)))

Output

Enter the size of the First List :: 6
Enter the size of the second List :: 5
Enter the Element of first List ::
1
2
3
4
5
6
Enter the Element of second List ::
4
5
6
7
8

Missing values in first list: {7, 8}
Additional values in first list: {1, 2, 3}
Missing values in second list: {1, 2, 3}
Additional values in second list: {7, 8}
raja
Published on 25-Sep-2018 14:17:27
Advertisements