Write a Python program to get the difference between the two list

Python: Difference between the two lists

Last update on October 08 2020 09:22:22 [UTC/GMT +8 hours]

Python | Difference between two lists

There are various ways in which the difference between two lists can be generated. In this article, we will see the two most important ways in which this can be done. One by using the set[] method, and another by not using it.

Examples:

Input : list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output : [10, 20, 30, 15] Explanation: resultant list = list1 - list2

Note: When you have multiple same elements then this would not work. In that case, this code will simply remove the same elements.
In that case, you can maintain a count of each element in both lists.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

By the use of set[]:

In this method, we convert the lists into sets explicitly and then simply reduce one from the other using the subtract operator. For more reference on set visit Sets in Python.



Example:

Python3




# Python code t get difference of two lists
# Using set[]
def Diff[li1, li2]:
return list[set[li1] - set[li2]] + list[set[li2] - set[li1]]
# Driver Code
li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
print[Diff[li1, li2]]

Output :

[10, 20, 30, 15]

Without using the set[]:

In this method, we use the basic combination technique to copy elements from both the list with a regular check if one is present in the other or not.

Example:

Python3




# Python code t get difference of two lists
# Not using set[]
def Diff[li1, li2]:
li_dif = [i for i in li1 + li2 if i not in li1 or i not in li2]
return li_dif
# Driver Code
li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
li3 = Diff[li1, li2]
print[li3]

Output :

[10, 20, 30, 15]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
python-list
python-set
Practice Tags :
python-list
python-set
Read Full Article

Get the Difference Between Two Lists in Python

Python Python List

Created: June-03, 2021 | Updated: July-09, 2021

There are many operations performed on data structures like lists, dictionaries, tuples, and arrays. In Python, these data structures are used in almost every program. One of these data structures is the lists in Python. Lists in Python programming helps in storing more than one item in just one variable. Finding out the difference between two lists is a very common task that almost every programmer carries out.

This tutorial will demonstrate several ways to get the difference between two lists in Python.

“difference between two lists python” Code Answer’s


2 list difference python
python by Mohamad76wp on Jun 05 2020 Comment
14
Source: kite.com
how to subtract 2 lists in python
python by Velvet Thunder
on Sep 21 2020 Comment
5
Source: stackoverflow.com
get list of words that are in two lists using set
r by Comfortable Cardinal on Nov 21 2020 Comment
0
Source: stackoverflow.com
Add a Grepper Answer

Python answers related to “difference between two lists python”

  • compare lists element wise python
  • comibataion of two list
  • dictionary from two lists
  • python compare each item of one list
  • how to compare the two key from constant value to list of string in python
  • python compine multiple lists
  • python same values in two lists
  • find matches between two lists python
  • find different values from two lists python
  • python difference between consecutive element in list
  • python list of difference beetwen values in list
  • find different between list
  • dictionary from two list
  • difference between two dictionaries python

Python queries related to “difference between two lists python”

  • python difference between two lists
  • difference between two lists python
  • how to subtract two lists in python
  • find difference between two lists python
  • difference between lists python
  • subtract 2 lists python
  • difference between 2 lists python
  • compare two lists in python and return the difference
  • python check difference between two lists
  • how to subtract 2 lists in python
  • subtract 2 elements of a list python
  • difference two lists python
  • python subtract two lists element wise
  • python symmetric difference of two lists
  • subtract two list in python
  • diff between two lists python
  • how to subtract elements of two lists in python
  • how to compare two lists in python and return differences
  • difference of 2 lists python
  • difference between lists
  • find differences between two lists python
  • python find the difference between two lists
  • use counter find differences between two lists
  • python array list difference
  • python difference list array
  • python subtract one list from another
  • set difference of two lists python
  • python get difference between two list
  • python difference list
  • get difference between lists python
  • python difference between lists
  • python subtract 2 lists
  • compare two list and print unique values python
  • can we subtract two lists in python
  • how to subtract one list from another in python
  • subtraction two lists python
  • difference in list python
  • find difference between 2 lists python
  • how to subtract two list python
  • list.difference python
  • difference between two list
  • python list subtraction
  • compare two lists and find differences python
  • how to subtract two list in python
  • find difference of two lists python
  • difference betweeen two arrys python
  • python subtract two lists elements
  • python difference two lists
  • python compare two lists for differences
  • how to get the difference between two lists
  • subtract two lists from each other python
  • difference of elements in 2 lists python
  • how to get difference between two lists in python
  • get difference between two lists
  • python unique values between 2 lists
  • diff between lists python
  • find differences between two lists
  • diffrence between 2 lists in python through sets
  • get the difference between two lists
  • difference of two lists
  • diff between 2 lists python
  • difference between two lists in python
  • difference of 2 lists
  • python list difference for list of lists
  • subtract two list elements python
  • python differences between two lists
  • symmetric difference between two list python
  • python check dfrerence beteen two lists
  • different elements between two lists python
  • difference two string lists python
  • python range between two list items
  • how to minus two list in python
  • find difference in two lists
  • find the difference bewtween two list in python
  • difference between 2 list in python
  • python subtract elements of two lists
  • two list difference add to first lists python
  • how to substract two list
  • find the difference between two list python
  • how to subtract list from another list in python
  • minus two lists python
  • list comprehension difference of two lists
  • find one to one difference between two lists python
  • python list difference between two lists
  • difference of each value of two lists python
  • how to get the list from the difference between 2 lists
  • subtract elements of two lists
  • compare two arrays in python and return the difference
  • python subtract lists element by element
  • how to find difference of two lists in python
  • how to list difference in python
  • how to compare each element in a list python for differences between two lists
  • difference python lists
  • python list subtract 2 list
  • how to substract 2 lists in python
  • difference of list in pandas
  • python number difference in a list
  • see two list difference
  • list minus list python
  • python subtract two lists from each other
  • python if difference between two lists
  • how to subtract things in a list in python
  • difference of two list in another list python
  • subtract two lists python with same length
  • how to subtract two lists of same size in python
  • subtract two lists number python
  • how to check for differences in two lists
  • how to get a list that is two lists subtracting in python
  • python subtracts 2 lists
  • python subtract two list elements
  • how to subtract a multiple in a list python
  • subscribe to two lists python
  • get difference of list of numbers python
  • how to find what is uncommon between two sets python
  • difference in list
  • how to subtract two lists in python numpy
  • python compare lists and set
  • apply symmetric diff between two list pandas
  • different elements in two lists python
  • subscribe two list in python
  • how subtract all of 2 list
  • how to subtract a list together in python
  • python list minus element
  • python diff lists
  • subtract in list using map in python
  • sub two list in python
  • sub two list python
  • python get difference value between two list strings
  • python code to subtract elements of two lists
  • how to substarct two lists python
  • how to subtract elements of two lists element wise
  • python subtrck two lists
  • substract 2 lists python element by element
  • get difference between two sets python
  • python subtract two lists of strings
  • different between two list
  • difference lists python
  • find difference between to arrays python
  • how to find the difference of two lists
  • get difference in two lists
  • how to find the diffrence between two lists in python
  • difference in lists python
  • array list python difference
  • python make diff between 2 lists
  • check difference lists python
  • print the difference of two sets python
  • python list difference of lists of lists
  • can we difference two list
  • take difference between two lists python
  • python function to take difference of two lists
  • python find diff between two lists
  • difference score of two lists python
  • get difference of 2 lists python
  • difference function list python
  • what is difference between two lists python
  • how to check in a list if difference is 2
  • python difference in two lists
  • set[list1] - set[list2]
  • difference of list
  • finding arrau dofferemce python
  • python differences of 2 lists
  • python compare lists difference
  • find array difference python
  • set dif between two list python
  • python any difference list
  • check differences between lists python
  • python list find difference between two values
  • list difference function python
  • python get 2 difference values in list
  • python difflib compare two lists
  • betwen 2 list diffrence python
  • diff of two list in python
  • two list diffrences
  • compare the two lists symmetrically in python
  • find all differences between two lists python
  • difference between list python
  • list and array difference python
  • find differences in 2 list pythpn
  • list differance with list py
  • python find difference between two arrays
  • difference function in list python
  • difference between two list of a list in python
  • of two lists compare and get differences python
  • show diff between 2 lists python
  • python get difference in two lists
  • show difference between two lists python
  • get difference between 2 lists python
  • pyhton diff between 2 lists
  • python compare list difference
  • how to find difference between two list inpython
  • difference between two lists in a list python
  • array list difference python
  • python3 list difference
  • python list of list of the difference in list items
  • difference between 2 lists
  • find diff between two lists python
  • check list difference python
  • how to find the difference of two lists python
  • difference of 2 elements in list python
  • difference in a list python
  • find difference in 2 lists python
  • difference of two list python
  • difference lists
  • python difference between list and print
  • arrays vs lists difference in python
  • calculate difference between two lists python
  • get difference in values between two lists python
  • python calculate difference between two lists
  • python check list difference
  • difference between 2 list python
  • how to find difference 1 between two lists in python
  • subtract two lists python
  • python list difference
  • get difference between two lists python
  • difference of two lists python
  • how to subtract lists in python
  • compare two lists python return difference
  • how to return diff in a list in python
  • set difference between two lists python
  • difference between two list python
  • python compare 2 lists and return the difference
  • python difference of two lists
  • difference python list
  • subtract elements of two lists python
  • subtracting 2 lists in python
  • subtract two lists in python
  • find difference between two lists
  • how to subtract all comms in a list python
  • python diff between two lists
  • how do you subrtact two lists in python
  • differences between two lists python
  • how to find the difference between two lists in python
  • get difference of two lists python
  • python compare 2 lists for differences
  • python list difference between lists
  • difference of list python
  • subtract two lists of strings python
  • get difference between two list python
  • how to find the difference between two list in python
  • python return difference between two lists
  • find difference of lists python
  • find list difference python
  • difference list python
  • python finding dfference between two groups
  • python array and list difference
  • subtraction in list python
  • python substract two lists
  • compare two lists to find differences python
  • check differences between two lists python
  • difference of 2 lists in python
  • subtract two list
  • compare 2 lists and find differences python
  • python print list differences
  • how to get difference of two lists in python
  • python get diff of two lists
  • difference list in python
  • subtract lists python
  • difference in two lists python
  • get the difference between 2 lists python
  • two list difference python
  • python subtract lists elementwise
  • difference between two python lists
  • subtract one list to another list python
  • find difference in two lists python
  • python find uncommon elements in two lists
  • subtraction of 2 lists in python
  • subtract a list from another list python
  • how to minus 2 lists in python
  • difference between elements of two list python
  • python function to add and minus elements of two lists
  • compare diff between 2 arrays in python
  • python how to subtract two lists
  • difference in two lists
  • python list array difference
  • python subtraction between two lisst
  • get ddifferenc 2d list python
  • python compare two lists and get difference
  • python diff two lists
  • find differences between two list python
  • or between lists python
  • how to compare two list and find different elements in python
  • find similarities between two lists python
  • how to check the different of 2 arrary in python
  • python differenc of to list
  • python subtract values of items in two lists
  • python 2 list difference
  • can we subtract two list
  • compare two lists and get differences python
  • how to diff two lists in python
  • take difference of two lists python
  • pandas find difference between two lists
  • find uncommon elements in two lists python
  • two list subtract python
  • two list difference elements
  • python find difference in two lists
  • python print difference between two list
  • get the difference of two lists python
  • can we difference of two list
  • define minus for lists python
  • select difference in list elements python
  • subtracting two list
  • difference operation in list in python
  • how to find the differences between two lists in python
  • compute the difference between two lists python
  • difference between 2 list
  • python program to find difference of two lists
  • python print difference between two lists
  • difference between element of two list in another list python
  • check difference of two lists python
  • how to find the difference between 2 list in python
  • find difference in two list python
  • how to compare 2 lists and find a difference python
  • find difference between two lists pandas
  • @ between 2 lists python
  • difference list in pyhton
  • subtract elements in two lists python
  • how to subtract element between 2 lists in python
  • subtract two lists element wise python
  • substract 2 list python
  • python subtract two lists set
  • how to subtract a list of lists by [3, 3] python
  • python subtract two lists elem
  • python subtract two lists by element
  • check how many different items between 2 arrays python
  • find difference between list of integers python
  • how to do differece between two list
  • python different values from two lists
  • python list difference method
  • python diff between list elements
  • finding difference of two lists python
  • how to subtract 2 list in python
  • find the difference between 2 elements in python
  • subtract two list from list python in one line
  • how to get the difference between two list elements in python
  • subtraction of two lists python
  • subtract 2 list elements python
  • subtract part of a list python?
  • sub two array in python
  • set minus with lists python
  • how to subtract one list from other python
  • subtract 2 elements in 1 list python
  • python subract two lists
  • how to substract element of two lists
  • diff between two list
  • subtract two lists python string
  • python substract 2 lists efficiently
  • python subtract each element of 2 lists
  • diff in 2 list python
  • difference of python lists
  • python3 get different in list
  • compare tow python list and show diff
  • compare 2 lists to find the difference python
  • compare two list
  • python list difference function
  • get difference of two list python
  • show the difference beewtween 2 sets python
  • getting the difference between two lists in python
  • python difference two list
  • make list of differences in a list python
  • difference between python lists
  • two lists showing value difference python
  • python get list with diffences
  • python finding difference between two lists
  • numpy difference between two lists
  • differnce of two lists python
  • python difference list elements
  • differences between two lists
  • difference of 2 list elemts
  • compare difference between two lists python
  • different elements between lists pyon
  • set function python between two elements of a list
  • find difference in two python lists
  • symmetric difference of two lists python
  • can + be used between two list
  • find differances between list
  • find all differences in a string list python
  • python take a difference out of two lists
  • different between two lists functions in python
  • get all different items from two lists python
  • how to get difference between two lists python
  • compare two lists find differences
  • set minus of two lists python
  • best way to get the difference between two arrays python
  • python compare two lists and find differences
  • find the difference in two lists in python
  • compare list python difference
  • python find difference between multiple lists
  • differences two lists python
  • diffrence between two lists python
  • python difference in 2 list
  • python find difference between lists
  • how to make difference of two lists in python
  • get differrence between two lists python
  • difference between two lists python output two lists
  • diff 2 lists in python
  • python vector difference list
  • différence liste python
  • find difference between two lists python
  • set difference lists python
  • how to get all values that are difference between two lists in python
  • see difference between two list python
  • python 2 lists difference
  • difference on lists in pyhton
  • difference in python between array a lists
  • how to find the difference of the values between two lists in python
  • python 2 lists differences
  • difference between elements of two lists python
  • get list difference python
  • return difference of two lists python
  • take difference of two lists
  • how to see a difference in a list in python
  • how to get difference between two elements of list in python
  • find difference in 2 lists
  • two list of strings difference python
  • python show difference between two lists
  • list elements difference python
  • python +diff 2 lists
  • check difference between two list of lists
  • get difference of list
  • difference array list python
  • compare two lists for differences python
  • how to get the difference between two list in python
  • find the differences between two lists python
  • list différence
  • python subtract two lists
  • python find difference between two lists
  • list difference python
  • python get difference between two lists
  • check difference between two lists python
  • subtract one list from another python
  • get the difference between two lists python
  • how to find difference between two lists in python
  • subtracting lists python
  • difference between two lists
  • 2 list difference python
  • find the difference between two lists python
  • difference of list in python
  • subtracting two lists python
  • difference between 2 lists in python
  • subtraction of two lists in python
  • python lists difference
  • difference between two list in python
  • return difference between two lists python
  • diff 2 lists python
  • difference between list python
  • diff of two lists python
  • list subtraction in python
  • how to subtract a list from another list in python
  • list difference
  • check the difference between two lists python
  • python subtraction between two lists
  • python get diff between two lists
  • return difference of two lists python
  • python get difference of two lists
  • how to subtract two list
  • python subtractt two lists
  • subtraction between lists python
  • difference of lists python
  • compare two lists python differences
  • get difference of lists python
  • how to get the difference of two lists in python
  • how to check difference between two lists in python
  • difference between list elements python
  • subtraction of lists in python
  • python difference between two list
  • list difference in python
  • difference two lists
  • finding difference between two lists in python
  • find the difference between two lists in python
  • python list subtract another list
  • 2 lists diff python
  • calculate difference in list python
  • difference of two list
  • python two list difference
  • how to get the difference between two lists in python
  • subtract integers in two lists python
  • list subtraction python
  • python find list difference
  • element wise difference between two lists python
  • subtract list from list python
  • get difference of list python
  • difference of lists
  • python get the difference between two lists
  • python get list difference
  • subtract two list python
  • find difference between two lists in python
  • python differents between arrays
  • substract two lists python
  • the difference between two lists python
  • difference between two arrays python
  • print difference between two lists python
  • difference between 2 string lists python
  • get delta of two lists python
  • show differences in lists python
  • python compare how many differences in 2 arrays
  • python difference of lists
  • difference list[:] and list
  • get diffrence between both list
  • subtraction between two lists python
  • compare two lists and find the different python
  • set difference between two list python
  • difference in list in python
  • python diff list
  • finding differences lists python3
  • write a python program to get the difference between the two lists.
  • python subtract list from another
  • find the difference between two list
  • operacions between two lists in python
  • get difference between two list in python
  • get difference to lists python
  • list of lists difference
  • substract lists in python
  • how to take difference of two lists in python
  • compare two arrays and return differences python
  • python zip lists and subtract
  • python diference betwen two lists in a list
  • element wise difference of two lists python
  • finding the difference between two lists python
  • how to get difference in 2 lists pythn
  • find the difference between two elements in a lists python
  • python get differences between two lists
  • python find the difference between two sets
  • python find amount differences in 2 list
  • python diff of two lists
  • python compare difference between two lists
  • difference between two numbers in list python
  • subtract 2 list python
  • python subtract values of items two lists
  • how to arithmatic subtract two list python
  • subract two lists python
  • subtract two lists python set
  • python substract 2 list
  • how to substract two lists in python
  • how to subtract 2 lists of string in python
  • subtract 2 list python
  • how to find uncommon data in two lists in python
  • python difference extract and return
  • how to find difference between two list containing integers in python
  • calculate time difference between two lists
  • python find values that difference between two lists
  • make an and among two list in python
  • subtract between two lists python
  • subtrat 2 lists python
  • how to subtract list place from another list in python
  • subtract two list from list python
  • subtracting two lists python
  • python diff list of ints
  • how to subtract one litst from another python string
  • python difference lists
  • subtraction between 2 lists
  • subtract corresponding element in python list
  • python3 subtract two lists
  • subtract 2 elements in a list python
  • can i subtract 2 lists python
  • python how to substract two lists
  • how to perform substration between two lists in python
  • how to subtract list 2 from list 1 in python
  • python substract 2 lists
  • python best way to compare arrays and find the differences
  • how to get the difference of list
  • difference between between two lists python
  • how to get the diff between two lists python
  • python code to find the difference between two list
  • python difference on list
  • python print[all[list1]] diff print[any[list2]]
  • how to do symmetric difference in python on lists
  • find different elements in two lists python
  • get list of differences between two lists python
  • python difference two lists
  • python find differences between two lists
  • two list difference
  • python difference between 2 lists
  • python3 diff between two list
  • find difference of list python
  • make a difference of list python
  • store the difference of lists python
  • python find the difference between two or more sets lists
  • python return the difference
  • making comparison between two lists python
  • get the list difference in python
  • pandas difference between two lists
  • how to check for differences in two arrays python
  • python get second difference between two lists
  • differences between lists in python
  • get difference between two lists python javascript
  • getting differnce in two list
  • python compare two lists and print differences
  • difference of two list in python
  • difference of a list python
  • difference between python list
  • python find difference if exist in list a not in
  • python compare two arrays find differences
  • difference of 2 arrays python
  • python 1 order difference list
  • lists and list difference
  • get diff between 2 list python
  • differ between 2 list
  • how to evaluate the difference between two lists
  • difference two list python
  • python difference between list of lists
  • check difference between to lists python
  • python difference of list elements
  • python get difference between two huge lists
  • difference 2 lists
  • differrence between two lists
  • difference between lists in python
  • compare two lists python difference
  • python difference of list
  • use difference on list in python
  • difference betwen lists
  • how to compare between 2 list in python
  • python difference between two number lists
  • do differenece between 2 lists
  • get difference of two listst
  • difference on lists
  • how to calculate the difference between two lists in python
  • how to get the difference between two list
  • python list of differences between list items
  • diff between two list python
  • find the difference between 2 lists python
  • get the difference between two lists in python
  • how to use difference using lists in python
  • python program to get the difference between the two lists
  • how to get difference between two elements of lists in python
  • python fastest way to find difference between two lists
  • compare list difference python
  • how to check the difference between two lists in python
  • difference list elements python
  • find differences between lists python
  • list difference python example
  • get differences between two list in python
  • python diff previous list
  • check difference btw 2 list python
  • python delta of two lists
  • python find all difference between two lists

Pythonic Ways to Find the Difference Between Two Lists

Python Set seems to be the most obvious choice to identify the common as well as the difference of two lists. So, we are going to explore it first and then will use nested loops and list comprehension.

Before we move on to the solution part, let’s define the test parameters, i.e., the two lists which we have to find the difference.

# Test Input list_a = [11, 16, 21, 26, 31, 36, 41] list_b = [26, 41, 36]

And we want our solution to provide the following output:

# Expected Result # list_out = list_a - list_b list_out = [11, 21, 31, 16]

Let’s start to create a program to find the difference between two lists, first using sets.

Use set[] to find the difference of two lists

In this approach, we’ll first derive two SETs [say set1 and set2] from the LISTs [say list1 and list2] by passing them to set[] function. After that, we’ll perform the set difference operation. It will return those elements from list1 which don’t exist in the second.

Here is the sample Python program to get the difference of two lists.

""" Desc: Using set[] to find the difference between two lists in Python """ def list_diff[list1, list2]: return [list[set[list1] - set[list2]]] # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print[list_diff[list1, list2]]

After running this, you should see the following outcome:

[16, 11, 21, 31]

Without set[], using nested loops

In this method, we’ll use nested For Loop to compare each element of the first list with the second. And while traversing, we’ll be appending every non-matching item to a new [and empty] list.

The new list would finally include the difference between the two given sequences. Below is the sample program to demonstrate this approach.

""" Desc: Nested loop to find the difference between two lists in Python """ def list_diff[list1, list2]: out = [] for ele in list1: if not ele in list2: out.append[ele] return out # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print[list_diff[list1, list2]]

After running the above program, you should see the following outcome:

[11, 16, 21, 31]

Without set[], using list comprehension

It is almost a similar technique that we used in the previous one. Here, we replaced the nested loops with the list comprehension syntax.

See the example below.

""" Desc: List comprehension to find the difference between two lists in Python """ def list_diff[list1, list2]: out = [item for item in list1 if not item in list2] return out # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print[list_diff[list1, list2]]

After running the above program, you should see the following outcome:

[11, 16, 21, 31]

We hope that after wrapping up this tutorial, you should know several ways to check two lists for the difference. However, you may practice more with examples to gain confidence.

Also, to learn Python from scratch to depth, do read our step by step Python tutorial.

1. Using set[] function

A simple solution is to convert both lists to set a data structure and then calculate the difference using the - operator.

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
first = [2, 1, 3, 4, 1]
second = [3, 4, 5]
# The `set[]` function is used to create sets
diff = list[set[first] - set[second]]
print[diff]# prints [1, 2]

DownloadRun Code

Video liên quan

Bài mới nhất

Chủ Đề