Open In App

# 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

## Python3

 `# 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}```

## note: install numpy module using command “pip install numpy”

Algorithm:

Convert list1 and list2 to numpy arrays using numpy.array() function.
Calculate the missing values in list2 using numpy.setdiff1d() with arguments arr1 and arr2.
Calculate the additional values in list2 using numpy.setdiff1d() with arguments arr2 and arr1.
Calculate the missing values in list1 using numpy.setdiff1d() with arguments arr2 and arr1.
Calculate the additional values in list1 using numpy.setdiff1d() with arguments arr1 and arr2.
Convert the numpy arrays to lists using list() function and return the results.

## Python3

 `import` `numpy as np`  `def` `find_missing_additional(list1, list2):``    ``# Convert list1 and list2 to numpy arrays``    ``arr1 ``=` `np.array(list1)``    ``arr2 ``=` `np.array(list2)` `    ``# Calculate missing and additional values in list2``    ``missing_list2 ``=` `np.setdiff1d(arr1, arr2)``    ``additional_list2 ``=` `np.setdiff1d(arr2, arr1)` `    ``# Calculate missing and additional values in list1``    ``missing_list1 ``=` `np.setdiff1d(arr2, arr1)``    ``additional_list1 ``=` `np.setdiff1d(arr1, arr2)` `    ``# Convert numpy arrays to lists and return the results``    ``return` `list``(missing_list2), ``list``(additional_list2), ``list``(missing_list1), ``list``(additional_list1)``list1 ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``]``list2 ``=` `[``4``, ``5``, ``6``, ``7``, ``8``]` `missing_list2, additional_list2, missing_list1, additional_list1 ``=` `find_missing_additional(list1, list2)``print``(``"Missing values in first list:"``, missing_list1)``print``(``"Additional values in first list:"``, additional_list1)``print``(``"Missing values in second list:"``, missing_list2)``print``(``"Additional values in second list:"``, additional_list2)`

Output:

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]

Time complexity:
The time complexity of the numpy.setdiff1d() function is O(nlogn) since it involves sorting the arrays. Therefore, the time complexity of this algorithm is O(nlogn) where n is the length of the input lists.

Auxiliary Space:
The space complexity of this algorithm is O(n) where n is the length of the input lists, as we are creating numpy arrays of length n.