Find Difference of Lists in Python

How to find the difference between two lists in Python? In Set theory, the difference between two sets can be defined as elements present in the first set, but not present in other sets. Here, we will discuss how to get differences in lists in python. Set iterable provides difference(), difference_update() methods to perform set difference, to use these you have to convert the list to set and utilize these methods and convert the resulting set to list. Besides these, there are other ways as well to find the difference between lists.

1. Quick Examples of List Difference

Following are quick examples of how to find the difference between two lists in python.


# Quick Examples of finding difference in lists

# Consider two lists
india_scores = [100,200,300,400,500]
australia_scores = [200,300,600,700,900]

# Using not in operator in for loop
result=[]
for i in india_scores:
    if i not in australia_scores:
        result.append(i)
print("List 1 - List 2: ",result)

# Using not in operator in List comprehension
result = [i for i in india_scores if i not in australia_scores]

# Using difference() by converting list to set
result = list(set(australia_scores).difference(set(india_scores)))

# Using numpy.setdiff1d()
import numpy as np
result = list(np.setdiff1d(australia_scores, india_scores))

# Using difference_update() by converting list to set
mylist1=set(india_scores)
mylist2=set(australia_scores)
mylist1.difference_update(australia_scores)
result = list(india_scores)

2. Python List Difference using Looping

Let’s find the difference between two lists in python by looping the lists and finding them. First, we will iterate the first list using for loop. Inside this, we will use the if statement to check if the element is not present in the second list or not by using the not in operator. If the element does not exist in the first list, then we will add that element to the result list.

2.1 for loop Syntax

Syntax of for loop to perform list difference operation.


# Syntax of using for loop
result=[]
for iterator in mylist1:
    if iterator  not in mylist2:
        result.append(iterator)

Here, mylist1 is the first input list and mylist2 is the second input list.

2.2 List Difference Example

Let’s create two lists of integers and find the difference between these two using for loop. The below example finds the elements that are present in match1 but not in match2 list.


# Consider two lists
match1 = [100,200,300,400,500]
match2 = [200,300,600,700,900]

# Using for loop
result=[]
for i in match1:
    if i not in match2:
        result.append(i)
print("List 1 - List 2: ",result)

# Output:
# List 1 - List 2:  [100, 400, 500]

Here, elements 100, 400 and 500 are present in match1, but not in match2 hence, these are the difference between match1 and match2 lists. Note that when you find difference of match2 with watch1 you will get the difference as 600, 700 and 900 (match2 elements that are not in match1).

3. Python List Differences by Comprehension

Similarly, you can also use list comprehension to find the difference in Python. First, we will iterate the first list using for loop inside a list comprehension. Inside this, we will use the if statement to check if the element is not present in the second list or not using the not-in operator. If the element does not exist in the first list, then we will add that element to the list comprehension.

3.1 Syntax

The syntax for list comprehension to perform find the difference.


# Syntax
[iterator for iterator in mylist1 if iterator not in mylist2]

Here, mylist1 is the first input list and mylist2 is the second input list.

3.2 List Difference Example

Let’s have two lists of integers and perform difference operation using comprehension. In the below examples, the first example return 100, 400, and 500 that are present in match1 but not in match2 and 600, 700 and 900 are present in match2, but not in match1.


# Consider two lists
match1 = [100,200,300,400,500]
match2 = [200,300,600,700,900]

# Using List comprehension
print("List 1 - List 2: ",[i for i in match1 if i not in match2])
print("List 2 - List 1: ",[i for i in match2 if i not in match1])

# Output:
# List 1 - List 2:  [100, 400, 500]
# List 2 - List 1:  [600, 700, 900]

4. Find List Difference using Set difference()

The set.difference() is the method that will return the elements present in the first set but not in the remaining sets. If we convert our lists to sets using set() method, we can use this method. Finally, using list() function we will convert set to list using list().

4.1 Syntax

Syntax for difference()


# Syntax
list(set(mylist1).difference(set(mylist2),set(mylist3),...))

4.2 Example

Let’s use the same list examples above and find the difference of these two lists. This example also yields the same output as above.


# Consider two lists
match1 = [100,200,300,400,500]
match2 = [200,300,600,700,900]

# Using difference() by converting list to set
result1 = list(set(match1).difference(set(match2)))
result2 = list(set(match2).difference(set(match1)))
print("List 1 - List 2: ",result1)
print("List 2 - List 1: ",result2)

# Output:
# List 1 - List 2:  [400, 100, 500]
# List 2 - List 1:  [600, 700, 900]

5. Using Set difference_update()

The difference_update() is the also set method that will return the elements present in the first set but not in the remaining sets. But the result set will be stored in the first set. Similar to the above, we convert our lists to sets, use this method, and convert the result set back to python list which gives the difference.

5.1 Syntax

Syntax for difference_update()


# Syntax
list(set(mylist1).difference_update(set(mylist2),set(mylist3),...))

5.2 List Difference Example

Let’s have three lists of strings and perform a difference operation by converting lists to sets and finding the difference of these three sets. For example, find the elements that are present in one list but not in other lists.


# Consider three lists
fruits1 = ["apple","mango"]
fruits2 = ["mango","orange"]
fruits3 = ["mango","water melon"]

# Convert lists to sets
set1=set(fruits1)
set2=set(fruits2)
set3=set(fruits3)

# Using difference_update() by converting list to set
set1.difference_update(set2,set3)
print("List 1 - List 2 - List 3: ",list(set1))

# Output:
# List 1 - List 2 - List 3:  ['apple']

Here, ‘apple‘ is the only string that exists in the first list but not in the second and the last.

6. Using numpy to find List Difference

The python numpy module provides the settdiff1d() method which can be used to perform a list difference operation. It is important to import numpy module.

6.1 Syntax

The syntax for numpy.setdiff1d()


# Syntax
list(np.setdiff1d(mylist1, mylist2,...))

6.2 Example

Let’s have two lists of integers and perform a difference operation busing numpy.setdiff1d().


# Import NumPy
import numpy as np
scores1 = [100,200,300,400,500]
scores2 = [200,300,600,700,900]

# Using numpy.setdiff1d()
print("List 1 - List 2: ",list(np.setdiff1d(scores1, scores2)))
print("List 2 - List 1: ",list(np.setdiff1d(scores2, scores1)))

# Output:
# List 1 - List 2:  [100, 400, 500]
# List 2 - List 1:  [600, 700, 900]

Here, the first example returned 100, 400 and 500 elements that are present in scores1, but not in scores2. And the second example returned 600, 700, and 900 elements that are present in scores2, but not in scores1.

7. Conclusion

You have learned different ways to find the difference between two lists in Python. If you are using difference() and difference_update() methods, you need to convert your lists to sets and perform these methods and finally convert the resulting set to a list using the list() function. It can also be possible to perform a difference operation using List comprehension and for loop with not in operator. You can also utilize numpy.setdiff1d() method to do this operation.

Reference

Leave a Reply