# Get difference between two Lists in Python

In this article, we will discuss different ways to get the difference between two lists in Python.

## Introduction

Suppose we have two lists,

```firstList = [33, 45, 27, 23, 48, 17, 10, 12, 11]
secondList = [11, 10, 77, 10, 34, 59, 11, 33, 27]
```

We want to get the differences between these two lists. Like this,

```[34, 12, 45, 77, 48, 17, 23, 59]
```

This list contains the differences between two lists. It has elements which exists in firstList only (12, 45, 48, 17, 23), and the elements which exists in secondList only (34, 77, 59).

There are different ways to achieve this. Let’s discuss them one by one.

## Method 1: Using Set

Compare both the lists with each other to get the differences. Steps are as follows,

• Create an empty set.
• Iterate over all the elements of first list, and for each element, check if it exists in the second list or not. If not, then append that to the Set created in first step.
• Iterate over all the elements of second list, and for each element, check if it exists in the first list or not. If not, then append that to the Set created in first step.
• Convert the set to the list. This list, will have all the difference between two given lists.

Let’s see an example to get the differences between two lists.

```firstList = [33, 45, 27, 23, 48, 17, 10, 12, 11]
secondList = [11, 10, 77, 10, 34, 59, 11, 33, 27]

# Create an empty set
diff = set()

# Iterate over all elements of first list
for elem in firstList:
# If element is not in second list, then add to set
if elem not in secondList:

# Iterate over all elements of second list
for elem in secondList:
# If element is not in first list, then add to set
if elem not in firstList:

# Convert set to list
diff = list(diff)

print(diff)
```

Output:

```[34, 12, 45, 77, 48, 17, 23, 59]
```

We created a list, containing the differences between two lists.

## Method 2: Using setdiff1d() method of NumPy Module

The NumPy module in Python, provides a function setdiff1d(arr1, arr2), to find the set difference of two arrays. It returns the unique values, which are in arr1, but not in arr2.

Instead of arrays, we can also pass lists in the setdiff1d() to get the differences between two lists. Steps are as follows,

• Get an array of unique values, which are in firstList, but not in secondList using setdiff1d().
• Get an array of unique values, which are in secondList, but not in firstList using setdiff1d().
• Concatenate both the arrays, to get the all the differences between firstList and secondList.

Let’s see an example,

```import numpy as np

firstList = [33, 45, 27, 23, 48, 17, 10, 12, 11]
secondList = [11, 10, 77, 10, 34, 59, 11, 33, 27]

# Get unique values, which are in firstList, but not in secondList
diff1 = np.setdiff1d(firstList, secondList)

# Get unique values, which are in secondList, but not in firstList
diff2 = np.setdiff1d(secondList, firstList)

# Concatenate both the arrays, to get the all
# the differences between firstList and secondList
diff = np.concatenate((diff1, diff2)).tolist()

print(diff)
```

Output:

```[12, 17, 23, 45, 48, 34, 59, 77]
```

We created a list, containing the differences between two lists.

## Method 3: Using symmetric_difference()

In Python, the Set class has a function symmetric_difference(). It accepts another Set as argument, and returns the symmetric difference of two sets as a new set. We can use this to get the differences between two lists. But for that, first we need to convert our lists to sets, and call the symmetric_difference() function. Let’s see an example,

```firstList = [33, 45, 27, 23, 48, 17, 10, 12, 11]
secondList = [11, 10, 77, 10, 34, 59, 11, 33, 27]

# Get differences between firstList and secondList
diff = list(set(firstList).symmetric_difference(set(secondList)))

print(diff)
```

Output:

```[34, 12, 45, 77, 48, 17, 23, 59]
```

We created a list, containing the differences between two lists.

## Summary

We learned about three different ways to get the differences between two lists in Python. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top