

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to list the difference between two lists.
In this problem given two lists. Our tasks is to display difference between two lists. Python provides set() method. We use this method here. A set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Example
Input::A = [10, 15, 20, 25, 30, 35, 40] B = [25, 40, 35] Output: [10, 20, 30, 15]
Explanation
difference list = A - B
Algorithm
Step 1: Input of two arrays. Step 2: convert the lists into sets explicitly. Step 3: simply reduce one from the other using the subtract operator.
Example Code
# Python code to get difference of two lists # Using set() def Diff(A, B): print("Difference of two lists ::>") return (list(set(A) - set(B))) # Driver Code A=list() n1=int(input("Enter the size of the first List ::")) print("Enter the Element of first List ::") for i in range(int(n1)): k=int(input("")) A.append(k) B=list() n2=int(input("Enter the size of the second List ::")) print("Enter the Element of second List ::") for i in range(int(n2)): k=int(input("")) B.append(k) print(Diff(A, B))
Output
Enter the size of the first List ::5 Enter the Element of first List :: 11 22 33 44 55 Enter the size of the second List ::4 Enter the Element of second List :: 11 55 44 99 Difference of two lists ::> [33, 22]
- Related Questions & Answers
- C# program to list the difference between two lists
- Program to find minimum difference between two elements from two lists in Python
- Program to interleave list elements from two linked lists in Python
- Python program to find difference between two timestamps
- Python program to create a sorted merged list of two unsorted lists
- Difference of two lists including duplicates in Python
- Python program to find Intersection of two lists?
- How to compare two lists and add the difference to a third list in C#?
- Python program to print all the common elements of two lists.
- C# Program to get the difference between two dates
- C# Program to return the difference between two sequences
- Java Program to Calculate the difference between two sets
- Python Program to Merge Two Lists and Sort it
- Python program to find Cartesian product of two lists
- Java Program to Join Two Lists
Advertisements