# Python: check if two lists are equal or not ( covers both Ordered & Unordered lists)

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,

- Both lists must contain the same unique elements and with same frequency, but elements can be placed in any order.
- 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,

first_list = [10, 10, 11, 12, 12, 13, 14, 16, 15, 16, 12] sec_list = [16, 12, 13, 14, 15, 16, 10, 11, 12, 10, 12]

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,

def check_if_equal(list_1, list_2): """ Check if both the lists are of same length and if yes then compare sorted versions of both the list to check if both of them are equal i.e. contain similar elements with same frequency. """ if len(list_1) != len(list_2): return False return sorted(list_1) == sorted(list_2)

first_list = [10, 10, 11, 12, 12, 13, 14, 16, 15, 16, 12] sec_list = [16, 12, 13, 14, 15, 16, 10, 11, 12, 10, 12] if check_if_equal(first_list, sec_list): print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal')

Output:

Lists are equal i.e. contain similar elements with same frequency

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,

collections.Counter(list)

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,

import collections def check_if_equal_2(list_1, list_2): """ Check if both the lists are of same length and then get the frequency of each element in list using collections.Counter. Then Compare if both the Counter objects are equal or not to confirm if lists contain similar elements with same frequency""" if len(list_1) != len(list_2): return False return collections.Counter(list_1) == collections.Counter(list_2) first_list = [10, 10, 11, 12, 12, 13, 14, 16, 15, 16, 12] sec_list = [16, 12, 13, 14, 15, 16, 10, 11, 12, 10, 12] if check_if_equal_2(first_list, sec_list): print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal')

Output:

Lists are equal i.e. contain similar elements with same frequency

### 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,

import numpy as np first_list = [10, 10, 11, 12, 12, 13, 14, 16, 15, 16, 12] sec_list = [16, 12, 13, 14, 15, 16, 10, 11, 12, 10, 12] # Convert both lists to sorted numpy arrays, then compare # them to check if both are equal or not result = np.array_equal(np.array(first_list).sort(), np.array(sec_list).sort()) if result: print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal')

Output:

Lists are equal i.e. contain similar elements with same frequency

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,

first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16]

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,

first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16] if first_list == sec_list: print('Lists are exactly equal') else: print('Lists are not equal')

Output:

Lists are exactly equal

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

first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16] # using map() & all() to check if both the lists are exactly equal result = all(map(lambda x, y: x == y, first_list, sec_list)) if result: print('Lists are exactly equal') else: print('Lists are not equal')

Output:

Lists are exactly equal

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

bool_list = list(map(lambda x, y: x == y, first_list, sec_list)) print(bool_list)

Output:

[True, True, True, True, True, True, True]

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,

result = all(bool_result) print(result)

Output:

True

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

from functools import reduce first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16] # using map() & reduce() to check if both the lists are exactly equal result = reduce(lambda a, b: a and b, map(lambda x, y: x == y, first_list, sec_list)) if result: print('Lists are exactly equal') else: print('Lists are not equal')

Output:

Lists are exactly equal

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

bool_list = list(map(lambda x, y: x == y, first_list, sec_list)) print(bool_list)

Output:

[True, True, True, True, True, True, True]

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,

- First it will apply the function on the first two values of the sequence and store the result as latest_result.
- 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.
- 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,

result = reduce(lambda a, b: a and b, bool_list) print(result)

Output:

True

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,

def check_if_exactly_equal(list_1, list_2): # check if both the lists are of same size if len(list_1) != len(list_2): return False # create a zipped object from 2lists final_list = zip(list_1, list_2) # iterate over the zipped object for elem in final_list: if elem[0] != elem[1]: return False return True

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

first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16] result = check_if_exactly_equal(first_list, sec_list) if result: print('Lists are exactly equal') else: print('Lists are not equal')

Output:

Lists are exactly equal

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

The complete example is as follows,

import collections from functools import reduce import numpy as np def check_if_equal(list_1, list_2): """ Check if both the lists are of same length and if yes then compare sorted versions of both the list to check if both of them are equal i.e. contain similar elements with same frequency. """ if len(list_1) != len(list_2): return False return sorted(list_1) == sorted(list_2) def check_if_equal_2(list_1, list_2): """ Check if both the lists are of same length and then get the frequency of each element in list using collections.Counter. Then Compare if both the Counter objects are equal or not to confirm if lists contain similar elements with same frequency""" if len(list_1) != len(list_2): return False return collections.Counter(list_1) == collections.Counter(list_2) def check_if_exactly_equal(list_1, list_2): # check if both the lists are of same size if len(list_1) != len(list_2): return False # create a zipped object from 2lists final_list = zip(list_1, list_2) # iterate over the zipped object for elem in final_list: if elem[0] != elem[1]: return False return True def main(): print('**** Check if two lists are equal irrespective of order of elements ****') first_list = [10, 10, 11, 12, 12, 13, 14, 16, 15, 16, 12] sec_list = [16, 12, 13, 14, 15, 16, 10, 11, 12, 10, 12] print('*** Sort & Compare to check if two lists are equal ***') if check_if_equal(first_list, sec_list): print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal') print('*** Using collections.Counter() to check if two lists are equal ***') if check_if_equal_2(first_list, sec_list): print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal') print('*** Using np.array_equal() to check if two lists are equal ***') # Convert both lists to sorted numpy arrays, then compare # them to check if both are equal or not result = np.array_equal(np.array(first_list).sort(), np.array(sec_list).sort()) if result: print('Lists are equal i.e. contain similar elements with same frequency') else: print('Lists are not equal') print('**** Check if two lists are exactly equal | Order of elements must be same ****') first_list = [10, 11, 12, 13, 14, 15, 16] sec_list = [10, 11, 12, 13, 14, 15, 16] print('*** Use == operator to check if two lists are exactly equal ***') if first_list == sec_list: print('Lists are exactly equal') else: print('Lists are not equal') print('*** Use map() and all() functions to check if two lists are exactly equal ***') # using map() & all() to check if both the lists are exactly equal result = all(map(lambda x, y: x == y, first_list, sec_list)) if result: print('Lists are exactly equal') else: print('Lists are not equal') print('*** Use reduce() and map() functions to check if two lists are exactly equal ***') # using map() & reduce() to check if both the lists are exactly equal result = reduce(lambda a, b: a and b, map(lambda x, y: x == y, first_list, sec_list)) if result: print('Lists are exactly equal') else: print('Lists are not equal') print('*** Iterate over two lists in parallel to check if two lists are exactly equal ***') result = check_if_exactly_equal(first_list, sec_list) if result: print('Lists are exactly equal') else: print('Lists are not equal') if __name__ == '__main__': main()

Output:

**** Check if two lists are equal irrespective of order of elements **** *** Sort & Compare to check if two lists are equal *** Lists are equal i.e. contain similar elements with same frequency *** Using collections.Counter() to check if two lists are equal *** Lists are equal i.e. contain similar elements with same frequency *** Using np.array_equal() to check if two lists are equal *** Lists are equal i.e. contain similar elements with same frequency **** Check if two lists are exactly equal | Order of elements must be same **** *** Use == operator to check if two lists are exactly equal *** Lists are exactly equal *** Use map() and all() functions to check if two lists are exactly equal *** Lists are exactly equal *** Use reduce() and map() functions to check if two lists are exactly equal *** Lists are exactly equal *** Iterate over two lists in parallel to check if two lists are exactly equal *** Lists are exactly equal

## Leave a Reply