Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Find missing and additional values in two lists
Next article icon

Python - Find missing and additional values in two lists

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

It's common to compare two lists and determine which values are missing or additional in each. For example, we might have a list of expected items and another list showing what we have in stock. We can easily identify missing and additional items by comparing the two lists. List comprehension is a compact and easy way to find missing and additional values. This method works best for smaller lists.

Python
a = [1, 2, 3, 4, 5, 6] b = [4, 5, 6, 7, 8]  # Missing values in a (values in b not in a) missing_in_a = [x for x in b if x not in a]  # Additional values in a (values in a not in b) additional_in_a = [x for x in a if x not in b]  # Missing values in b (values in a not in b) missing_in_b = [x for x in a if x not in b]  # Additional values in b (values in b not in a) additional_in_b = [x for x in b if x not in a]  print(missing_in_a) print(additional_in_a) print(missing_in_b) print(additional_in_b) 

Output
[7, 8] [1, 2, 3] [1, 2, 3] [7, 8] 

Other methods to find missing and additional values in two lists:

Table of Content

  • Using Sets
  • Using NumPy
  • Using Loops

Using Sets (Efficient for Larger Lists)

Sets in Python allow us to perform efficient set operations like difference and union. This method is faster than list comprehension when dealing with larger lists.

Python
a = [1, 2, 3, 4, 5, 6] b = [4, 5, 6, 7, 8]  # Convert lists to sets set_a = set(a) set_b = set(b)  # Missing values in a (elements in b not in a) missing_in_a = list(set_b - set_a)  # Additional values in a (elements in a not in b) additional_in_a = list(set_a - set_b)  # Missing values in b (elements in a not in b) missing_in_b = list(set_a - set_b)  # Additional values in b (elements in b not in a) additional_in_b = list(set_b - set_a)  print(missing_in_a) print(additional_in_a) print(missing_in_b) print(additional_in_b) 

Output
[8, 7] [1, 2, 3] [1, 2, 3] [8, 7] 

Using NumPy (Efficient for Large Datasets)

For numerical data, NumPy is a powerful library that can make comparisons very fast. NumPy’s array operations are much faster than Python lists, especially when the data is large.

Python
import numpy as np  # List 1 and List 2 a = np.array([1, 2, 3, 4, 5, 6]) b = np.array([4, 5, 6, 7, 8])  # Missing values in a (values in b not in a) missing_in_a = np.setdiff1d(b, a)  # Additional values in a (values in a not in b) additional_in_a = np.setdiff1d(a, b)  # Missing values in b (values in a not in b) missing_in_b = np.setdiff1d(a, b)  # Additional values in b (values in b not in a) additional_in_b = np.setdiff1d(b, a)  print(missing_in_a) print(additional_in_a) print(missing_in_b) print(additional_in_b) 

Output
[7 8] [1 2 3] [1 2 3] [7 8] 

Using Loops

If we want to manually compare the two lists, we can use loops. This method is slower for large lists but helps understand the process clearly.

Python
a = [1, 2, 3, 4, 5, 6] b = [4, 5, 6, 7, 8]  # Empty lists to store the missing and additional values missing_in_a = [] additional_in_a = [] missing_in_b = [] additional_in_b = []  # Check for missing and additional values in both lists for x in a:     if x not in b:         missing_in_b.append(x)  for x in b:     if x not in a:         missing_in_a.append(x)  print(missing_in_a) print(additional_in_a) print(missing_in_b) print(additional_in_b) 

Output
[7, 8] [] [1, 2, 3] [] 

Next Article
Python - Find missing and additional values in two lists

S

Striver
Improve
Article Tags :
  • Misc
  • Python
  • python-list
  • python-set
  • Python list-programs
  • Python set-programs
Practice Tags :
  • Misc
  • python
  • python-list
  • python-set

Similar Reads

    Remove all values from a list present in another list - Python
    When we work with lists in Python, sometimes we need to remove values from one list that also exists in another list. Set operations are the most efficient method for larger datasets. When working with larger lists, using sets can improve performance. This is because checking if an item exists in a
    3 min read
    Python | Extract Combination Mapping in two lists
    Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method
    2 min read
    Check if two lists are identical in Python
    Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).Using Equality Operator (==)The easiest way to check if two lists are iden
    2 min read
    Merge Two Lists in Python
    Python provides several approaches to merge two lists. In this article, we will explore different methods to merge lists with their use cases. The simplest way to merge two lists is by using the + operator. Let's take an example to merge two lists using + operator.Pythona = [1, 2, 3] b = [4, 5, 6] #
    4 min read
    Python | Intersection of two lists
    The task of finding the intersection of two lists involves identifying the common elements between them. This means we want to extract the values that appear in both lists, while ignoring any duplicates or values that are unique to each list. For example, if we have two lists [4, 9, 1, 17, 11] and [
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences