How to compare lists in Python

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.

Output:

True
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.

Output:

Equal

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,

Output:

True
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.

Output:

Equal

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.

Output:

Equal

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,

Output:

True

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.

Output:

Equal

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,

Output:

Equal

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *