Open In App

Python | Find missing and additional values in two lists

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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}

Approach using numpy:

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.



Similar Reads

Python | Remove additional spaces in list
Sometimes, we have a list that contains strings and spaces in between them. We desire to have uniformity, so that later if we decide them, we just have single spaces between the lists. Hence its sometimes necessary to remove the additional unnecessary spaces between the words in a list. Let's discuss certain ways in which this can be done. Method #
3 min read
Python - Test for Even values dictionary values lists
Given a dictionary with lists as values, map Boolean values depending upon all values in List are Even or not. Input : {"Gfg" : [6, 8, 10], "is" : [8, 10, 12, 16], "Best" : [10, 16, 14, 6]} Output : {'Gfg': True, 'is': True, 'Best': True} Explanation : All lists have even numbers. Input : {"Gfg" : [6, 5, 10], "is" : [8, 10, 11, 16], "Best" : [10, 1
8 min read
How to Pass Additional Context into a Class Based View (Django)?
Passing context into your templates from class-based views is easy once you know what to look out for. There are two ways to do it - one involves get_context_data, the other is by modifying the extra_context variable. Let see how to use both the methods one by one. Explanation: Illustration of How to use get_context_data method and extra_context va
2 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Replacing missing values using Pandas in Python
Dataset is a collection of attributes and rows. Data set can have missing data that are represented by NA in Python and in this article, we are going to replace missing values in this article We consider this data set: Dataset In our data contains missing values in quantity, price, bought, forenoon and afternoon columns, So, We can replace missing
2 min read
How to handle missing values of categorical variables in Python?
Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. Often we come across datasets in which some values are missing from the columns. This causes problems when we apply a machine learning model to the dataset. This increases the chances of error when we are training the machine lea
4 min read
How to deal with missing values in a Timeseries in Python?
It is common to come across missing values when working with real-world data. Time series data is different from traditional machine learning datasets because it is collected under varying conditions over time. As a result, different mechanisms can be responsible for missing records at different times. These mechanisms are known as missingness mech
10 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
Indexing Lists Of Lists In Python
Lists of lists are a common data structure in Python, providing a versatile way to organize and manipulate data. When working with nested lists, it's crucial to understand how to index and access elements efficiently. In this article, we will explore three methods to index lists of lists in Python using the creation of a sample list, followed by ex
3 min read