Table of Contents

In this post, we will see how to compare lists in Python.

## Python compare lists

Lists in Python are mutable and can store a finite number of elements. We cannot determine whether a list is greater or less than any other list based on general criteria. So when we talk about comparing lists, we mean checking whether two lists have the same elements or not.

## How to compare lists in Python

In this article, several ways are discussed to compare two lists in Python. Two lists can only be equal if they are of the same length so in this article all examples assume that the lists being compared are of the same length.

### Using the `sort()`

function and `==`

operator

We can use the equality operator (`==`

) to compare two lists and check whether they are equal or not. It is advisable to use the `sort()`

function before comparing because sometimes the lists may contain the same elements but they might be shuffled. The `sort()`

function will sort them. Also, this function sorts the original list and does not create a new list.

See the code below.

1 2 3 4 5 6 7 8 9 10 |
a = [1,5,9] b = [5,9,1] c = [1,6,9] a.sort() b.sort() c.sort() print(a==b) print(a==c) |

Output:

False

In the above example,

- We created three lists
`a`

,`b`

, and`c`

. - We sorted the lists and compared them.
- Lists
`a`

and`b`

were equal and`True`

is returned. - Lists
`a`

and`c`

were not equal so we get`False`

. - The
`sort()`

function can be used with any of the methods discussed below.

### Using the `map()`

and `reduce()`

functions

We can implement a simple `lambda`

function to compare two lists. Such functions are used to implement simple, one-line logic.

For this, we will use the `map()`

and `reduce()`

functions. The `map()`

function takes an iterable and applies a function to every element of this object. The `reduce()`

function will be applied to the map object to compare the two elements.

It is necessary to sort the lists before using these functions.

We implement the above logic in the following code snippet.

1 2 3 4 5 6 7 8 9 10 11 |
import functools a = [1,5,9] b = [5,9,1] a.sort() b.sort() if functools.reduce(lambda h, k: h and k, map(lambda x, y: x == y, a, b), True): print("Equal") else: print("Unequal") |

Output:

### Using the `set()`

function and `==`

operator

The `set()`

function returns a set object that is unordered and contains distinct elements. We can compare these sets using the `==`

operator.

There is no requirement to sort these lists a set is unordered.

For example,

1 2 3 4 5 6 7 |
a = [1,5,9] b = [5,9,1] c = [1,6,9] print(set(a) == set(b)) print(set(a) == set(c)) |

Output:

False

In the above code,

- We first created three lists.
- These lists were converted to sets using the
`set()`

function. - These sets were compared for equality using the
`==`

operator.

### Using the `for`

loop

We can use the `for`

loop to iterate over both lists and compare each element individually. If even a single pair of elements do not match, we break out of the loop and know that the lists are not equal.

See the code below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
a = [1,5,9] b = [5,9,1] a.sort() b.sort() f = 0 for i in range(len(a)): if a[i]!=b[i]: f = 1 break if(f==0): print("Equal") else: print("Not Equal") |

Output:

### Using the `set()`

function and `-`

operator

We can convert two lists to a set using the `set()`

function and calculate their difference. The difference between two the sets is the total elements present in the first set and not in the second set. We can use this difference to compare the two lists. If the difference is an empty set, they are equal, else they are not.

See the following example.

1 2 3 4 5 6 7 8 9 |
a = [1,5,9] b = [5,9,1] d = (set(a) - set(b)) if len(d)==0: print("Equal") else: print("Not Equal") |

Output:

### Using the `collections.Counter()`

class and `==`

operator

The `Counter`

is a sub-class from the `collections`

module that stores elements in a hashable object with a dictionary-like structure. We can store two lists in such objects and compare them using the `==`

operator.

No requirement to sort the lists beforehand.

For example,

1 2 3 4 5 6 |
from collections import Counter a = [1,5,9] b = [5,9,1] print(Counter(a)==Counter(b)) |

Output:

### Using the `zip()`

, `sum()`

, and `len()`

functions

The `zip()`

function returns a zip-type object by combining elements of two iterables. The `len()`

function calculates the length of the iterable.

We can use these functions with the `sum()`

function to calculate the total number of indexes where the elements are different. If this sum is 0, then the lists are equal, else they are not equal. It is essential to sort the lists before using these functions.

We implement this logic in the following code snippet.

1 2 3 4 5 6 7 8 9 10 11 12 |
a = [1,5,9] b = [5,9,1] a.sort() b.sort() d = sum(x != y for x, y in zip(a,b)) d += abs(len(a) - len(b)) if(d==0): print("Equal") else: print("Not Equal") |

Output:

### Using the `cmp()`

function

The `cmp()`

function returns 1,-1, and 0 based on the comparison between two integers. We can use it with lists, and if the result is 0 then the two lists are equal.

This function works only in Python 2.

For example,

1 2 3 4 5 6 7 8 9 10 |
a = [1,5,9] b = [5,9,1] a.sort() b.sort() if(cmp(a,b) == 0): print "Equal" else: print("Not Equal") |

Output:

## Further reading:

## Conclusion

In this article, we discussed different ways to compare two lists in Python. The `==`

operator is used in many methods, and we discovered some innovative ways to achieve the same using sets, `cmp()`

, and `zip()`

methods.