In this article we will discuss 8 different ways to check if two lists are equal or not.

Suppose we have two lists and we want to check if both the lists are equal or not. There can be two meanings of 2 equality here,

  1. Both lists must contain the same unique elements and with same frequency, but elements can be placed in any order.
  2. Both lists must be exactly equal i.e. order of elements must be the same.

Let’s discuss both the scenarios one by one,

Check if two lists are equal irrespective of order of elements

Suppose we have two lists,

Both the lists contain similar elements with the same frequency, but the order of elements are different in them. There are different ways to check if these kinds of lists are equal or not,

Sort & Compare to check if two lists are equal

We can create sorted versions of both the lists. If original lists contain the same elements but in different order, then the order of elements must be similar in sorted versions of the lists. So, by comparing sorting versions of lists we can find out if lists are equal or not. For example,

Output:

First it checked if both the lists are of the same size or not. If both are of different size then it means lists are not equal. Whereas, if both lists are of same size, then created a sorted versions of both the lists and compared them using == operator to check if lists are equal or not.

So this is how we can compare two lists irrespective of order of elements in the list.

Using collections.Counter() to check if two lists are equal

collections module provides a Counter class. We can create its object by passing a list to its constructor,

It returns a Counter object, which is a dict subclass and it contains the frequency count of each unique element in the list. So, to check if two  lists are equal or not, we can create Counter objects from both the lists and then compare them to check if both lists contain similar unique elements with the same frequency.

For example,

Output:

Using np.array_equal() to check if two lists are equal

We can create two sorted numpy arrays from our lists and then we can compare them using numpy.array_equal() to check if both contains same elements. For example,

Output:

This approach can be useful if we want to compare a list with a numpy array or multidimensional arrays.

Check if two lists are exactly equal | Order of elements must be same

Suppose we have two lists,

Now we want to check if both the lists are exactly equal or not i.e. if both lists contain the same elements and that too in same order. For that we need to compare both the lists element by element. There are different ways to do that and let’s discuss them one by one,

Use == operator to check if two lists are exactly equal

We can directly compare two lists using == operator. If both the lists are exactly equal them it will return True else False,

Output:

It is the easiest and quickest way to check if both the lists are exactly equal. But it is good to know some other options too.

Use map() and all() functions to check if two lists are exactly equal

Output:

How did it work ?

map() function applies the given lambda function to each element of both the lists and stores the result to a new array. Which in our case will be a bool array because inside the lambda function we are checking if both the elements are equal or not.

Output:

The map() function returns a new bool array, where each ith entry in this bool array represents if first_list[i] is equal to sec_list[i] or not. If all the elements in this bool array are True then it means both lists are equal. We can do this using all() function,

Output:

The function all() accepts a sequence as an argument and returns True if all the elements in this sequence are True. So, this is how we can check if two lists are exactly equal.

Use reduce() and map() functions to check if two lists are exactly equal

Output:

How did it work ?
The map() function applies the given lambda function to each element of both the lists and stores the result to a new array. Which in our case will be a bool array because inside the lambda function we are checking if both the elements are equal or not.

Output:

The map() function returns a new bool array, where each ith entry in this bool array represents if first_list[i] is equal to sec_list[i] or not. If all the elements in this bool array are True then it means both lists are equal. We can do this using reduce() function,

The function reduce(fun,seq) accepts a function and a sequence as arguments. It returns a single value by this logic,

  1. First it will apply the function on the first two values of the sequence and store the result as latest_result.
  2. Then it will again call the function and pass latest_result along with the next item in the sequence and store the result as latest_result.
  3. It will repeat step 2 till all elements of sequence are consumed.

We can use reduce() function to check if a bool list contain all True elements or not,

Output:

So, this is how we can check if two lists are exactly equal.

Iterate over two lists in parallel to check if two lists are exactly equal

We can iterate over both the lists in parallel using for loop. For that we will zip both the list objects to create a list of tuples. Where ith tuple in this list of tuples contains the ith element of both the lists i.e. (list_1[i], list_2[i]). Then we can iterate over this list of tuples to check if both the elements in each tuple are the equal or not. If each tuple contains equal elements in this list of tuples then it means both the lists are equal.

We have created a separate function to perform this logic,

Now let’s use this function to check if both the lists are equal or not,

Output:

These were 8 different ways to check if two lists are equal or not in python.

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.