# [6 Ways] Compare Two Lists in Python and Return Non-Match Elements

List preserves the order of the elements in the list. When you compare the two lists using “==” operator, it returns True if all the elements in the lists are the same and in the same order.

```list_a = [2, 4, 6, 8]
list_b = [2, 4, 6, 8]

print(list_a==list_b)```

Output:

`True`

If you change the order of the elements in any of the list and then compare it with other, it returns False.

```list_a = [2, 4, 6, 8]
list_b = [2, 8, 6, 4]

print(list_a==list_b)```

Output:

`False`

Now we want to compare the two list irrespective of their order.

### Compare Two Lists in Python

#### 1. Using Membership Operator

We can compare the list by checking if each element in the one list present in another list.

```def is_identical(list_a, list_b):
if len(list_a) != len(list_b):
return False
for i in list_a:
if i not in list_b:
return False
return True

list_a = [2, 4, 6, 8]
list_b = [2, 8, 6, 4]

if is_identical(list_a, list_b):
print("Two lists are identical.")
else:
print("Two lists are different.")```

Output:

`Two lists are identical.`

This is a tedious job as we have to traverse through the complete list.

You have to take one element from the list ‘A’ and needs to search if the element present in list ‘B’.

As the list is not sorted by default, for each element, it will take `O(n)` time to search the element in another list.

So, the total complexity will be `O(n^2)`.

This is good for learning and definitely not the standard way of writing the Python code.

Can we optimize this problem?

Basically we need to find out the solution to solve this problem without traversing the complete list.

There is way of doing this. We can use the Python inbuilt functions for comparing two lists.

Let’s see.

#### 2. Using Set Method

To compare two lists, we are using the set method.

• If the length of the two lists is different, the list can not be identical and return False.
• Else, Convert both the lists into sets.
• Compare these two sets. If the sets are equal, two given lists are the same. Otherwise, two lists are different.
```def is_identical(list_a, list_b):
if len(list_a) != len(list_b):
return False
if set(list_a) == set(list_b):
return True
else:
return False

list_a = [2, 4, 6, 8]
list_b = [2, 8, 6, 4]

if is_identical(list_a, list_b):
print("Two lists are identical.")
else:
print("Two lists are different.")```

Output:

`Two lists are identical.`

Drawback of using set() method:

The set() method removes all the duplicate elements in the list. If your list contains duplicate elements, this solution will not work for you.

Let’s see how you can take a rid of this problem to compare two lists in Python.

#### 3. Using Sort Method

We can sort the two given list and then compare.

We have already learned about two ways of sorting the Python list in the previous tutorial. Let’s use that to find if the two lists are the same.

```list_a = [2, 4, 6, 6]
list_b = [2, 6, 4, 6]

def is_identical(list_a, list_b):
list_a.sort()
list_b.sort()
return list_a == list_b

if is_identical(list_a, list_b):
print("Two lists are identical.")
else:
print("Two lists are different.")```

Output:

Two lists are identical.

This solution will work even if the lists have duplicate elements.

Now, we are increasing the scope of our problem. Instead of checking if the two lists are the same or not, we also want to find the non-match elements in the list.

#### 4. Return Non-Matches Elements with For Loop

You can write a function to return all unmatched elements in the list using for loop.

```def non_match_elements(list_a, list_b):
non_match = []
for i in list_a:
if i not in list_b:
non_match.append(i)
return non_match

list_a = [2, 4, 6, 8, 10, 12]
list_b = [2, 4, 6, 8]

non_match = non_match_elements(list_a, list_b)
print("No match elements: ", non_match)```

Output:

`No match elements: [10, 12]`

Instead of just for loop, you can use other looping mechanisms in Python.

You can also modify the above code to return the index of non-match elements in the list.

#### 5. Difference Between Two List

We can perform the subtraction operation on set to find the extra elements in one list from another list.

```def get_difference(list_a, list_b):
return set(list_a)-set(list_b)

list_a = [2, 4, 6, 8, 10, 12]
list_b = [2, 4, 6, 8]

non_match = list(get_difference(list_a, list_b))
print("No match elements: ", non_match)```

Output:

`No match elements: [10, 12]`

Now, let’s find the unmatched elements from both the list.

```def get_difference(list_a, list_b):
non_match_a  = set(list_a)-set(list_b)
non_match_b  = set(list_b)-set(list_a)
non_match = list(non_match_a) + list(non_match_b)
return non_match

list_a = [2, 4, 6, 8, 10, 12]
list_b = [2, 4, 6, 8, 9, 13]

non_match = get_difference(list_b, list_a)
print("Non-match elements: ", non_match)```

Output:

`Non-match elements: [9, 13, 10, 12]`

This will give you all the unmatched elements from both the lists.

Let’s make it more interesting with some of the Python advanced concepts.

#### 6. Lambda Function To Return All Unmatched Elements

We can rewrite the above regular function using the lambda function to get all the non-matched elements in the lists.

```l_func = lambda x, y: list((set(x)- set(y))) + list((set(y)- set(x)))

list_a = [2, 4, 6, 8, 10, 12]
list_b = [2, 4, 6, 8, 9, 13]

non_match = l_func(list_a, list_b)

print("Non-match elements: ", non_match)```

Output:

`Non-match elements: [10, 12, 9, 13]`

Python is interesting and you can find many ways of comparing two lists.

### Use-cases

I was working on one of the projects where I’m calling REST API. After getting a web response form REST API, I have to validate all the HTTP header fields whether all the required attributes are present or not.

The easiest way is to keep the list of all the expected attributes and then compare it with the list of attributes getting from the REST API response. If the two lists are not the same, through an error telling “attribute missing.”

Similarly, you can find the many other use-cases where you need to compare two or more Python list to find unmatched elements..

Other List Related Tutorials for Practice:

In this tutorial, we have learned different methods to compare two lists in Python and return non match elements. If you have any doubts or if you know any other way of comparing two lists, share it with me in the comment.

Happy Pythoning!

1. Nandu says:
```def get_difference(list_a, list_b):
non_match_a  = set(list_a)-set(list_b)
non_match_b  = set(list_a)-set(list_b)
non_match = list(non_match_a) + list(non_match_b)
return non_match

list_a = [2, 4, 6, 8, 10, 12]
list_b = [2, 4, 6, 8, 9, 13]

non_match = get_difference(list_b, list_a)
print("Non-match elements: ", non_match)
```
`For this o/p is [9, 13, 9, 13] not [10, 12, 9, 13] `

Reason :

```non_match_a  = set(list_a)-set(list_b)
non_match_b  = set(list_a)-set(list_b)
```

it should be

```
non_match_a  = set(list_a)-set(list_b)
non_match_b  = set(list_b)-set(list_a)
```
2. Simon H. says:

[].sort() does not return anything (https://docs.python.org/3/library/stdtypes.html#list.sort)

```def is_identical(list_a, list_b):
return list_a.sort() == list_b.sort()
```

can be written as:

```def is_identical(list_a, list_b):
return sorted(list_a) == sorted(list_b)
```

or

```def is_identical(list_a, list_b):
list_a.sort()
list_b.sort()
return list_a.sort() == list_b.sort()
# the original of list_a, list_b are sorted now
```
1. Lea Joseph Puthumana says:

I have a similar problem to solve. Can you help?

```  first_set = set(map(tuple, coords))
second_set = set(map(tuple, coords1))
similar=first_set.intersection(second_set)

print(first_set)
print(second_set)
print(similar)
```

Here in this code, I found the exact same corners.

eg. My first set is (133, 708), (281, 287), (140, 396), (285, 550)

Second set is (295, 315), (213, 552), (181, 643), (255, 396), (282, 552)

Difference of 1st set coordinates and second set coordinates if <=5

ie, last set of first & the second set is printed as similar corners.

while (282-285,552-550) = (3,0) .Here difference is (3,0) .

Similar corners of first & second set are (285, 550) & (282, 55

3. Chris says:

How do I take this one step further in complexity? For example, if list a = `[ (date, code), (date code), (date, code)...]`
and b = `[(date, code), (date, code), (date, code)...]`, and what I want to do is delete the pairs where the codes are the same but some of the dates in b greater than dates in a

Thanks, sir!