Python | Find missing and additional values in two lists
Given two lists, find the missing and additional values in both the lists.
Examples:
Input : list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8]
Output : Missing values in list1 = [8, 7]
Additional values in list1 = [1, 2, 3]
Missing values in list2 = [1, 2, 3]
Additional values in list2 = [7, 8]
Explanation:
Approach: To find the missing elements of list2 we need to get the difference of list1 from list2. To find the additional elements of list2, calculate the difference of list2 from list1.
Similarly while finding missing elements of list1, calculate the difference of list2 from list1. To find the additional elements in list1, calculate the difference of list1 from list2.
Insert the list1 and list2 to set and then use difference function in sets to get the required answer.
Prerequisite : Python Set Difference
# Python program to find the missing # and additional elements # examples of lists list1 = [1, 2, 3, 4, 5, 6] list2 = [4, 5, 6, 7, 8] # prints the missing and additional elements in list2 print("Missing values in second list:", (set(list1).difference(list2))) print("Additional values in second list:", (set(list2).difference(list1))) # prints the missing and additional elements in list1 print("Missing values in first list:", (set(list2).difference(list1))) print("Additional values in first list:", (set(list1).difference(list2))) |
Output:
Missing values in second list: {1, 2, 3}
Additional values in second list: {7, 8}
Missing values in first list: {7, 8}
Additional values in first list: {1, 2, 3}
Recommended Posts:
- Python | Visualize missing values (NaN) values using Missingno Library
- Python | Split dictionary keys and values into separate lists
- Python | Find missing elements in List
- Python | Find missing numbers in a sorted list range
- Python | Remove additional spaces in list
- Python | Find indices with None values in given list
- Python | Find number of lists in a tuple
- Python | Find keys with duplicate values in dictionary
- Python program to find the highest 3 values in a dictionary
- Python | Find minimum of each index in list of lists
- Python | Find common elements in list of lists
- Python | Find frequency of given character at every position in list of lists
- Python program to find common elements in three lists using sets
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




