# Python: Check if Two Lists are Equal

Multiple items may be stored in a single variable using lists.

Lists are one of four built-in Python storage types for storing sets of data; the other three are Tuple, Set, and Dictionary, all of which have different qualities and applications. You can also find difference between two lists python

Square brackets[] are used to create a list

Examples:

Comparing lists irrespective of order of lists:

Input:

firstlist = ['hello', 'this', 'is', 'BTechGeeks']

secondlist = ['this', 'is', 'BTechGeeks','hello']

Output:

Both lists are equal

Input:

firstlist = ['hello', 'this', 'is', 'BTechGeeks','is']

secondlist = ['this', 'is', 'BTechGeeks','hello','the']

Output:

Both lists are not equal

Comparing list based on order of lists:

Input:

firstlist = ['this' ,'is','BTechGeeks']
secondlist = ['this' ,'is','BTechGeeks']

Output:

Both lists are equal

Input:

firstlist = ['hello', 'this' ,'is','BTechGeeks']
secondlist = ['this' ,'is','BTechGeeks']

Output:

Both lists are not equal

## Check if Two Lists are Equal or not

There are several ways to check if two lists are equal or not some of them are:

### Method #1:Using sorting()

We start by sorting the list so that if both lists are similar, the elements are in the same place. However, this ignores the order of the elements in the list. So, by comparing sorting versions of lists, we can determine whether or not they are equal.

Below is the implementation:

# function to check both lists if they are equal
def checkList(firstlist, secondlist):
# sorting the lists
firstlist.sort()
secondlist.sort()
# if both the lists are equal the print yes
if(firstlist == secondlist):
print("Both lists are equal")
else:
print("Both lists are not equal")

# Driver code
# given two lists
firstlist = ['hello', 'this', 'is', 'BTechGeeks']
secondlist = ['this', 'is', 'BTechGeeks','hello']
# passing both the lists to checklist function
checkList(firstlist, secondlist)


Output:

Both lists are not equal

### Method #2:Using Counter() function

We can normally get the frequency of each variable in a list using Counter(), and we can search for it in both lists to see whether they are similar or not. However, this approach disregards the order of the elements in the list and only considers the frequency of the elements.

Below is the implementation:

# importing counter function from collections
from collections import Counter

# function to check both lists if they are equal
def checkList(firstlist, secondlist):
# Getting frequencies of both lists
firstfreq = Counter(firstlist)
secondfreq = Counter(secondlist)
# if both the lists are equal the print yes
if(firstfreq == secondfreq):
print("Both lists are equal")
else:
print("Both lists are not equal")

# Driver code
# given two lists
firstlist = ['hello', 'this', 'is', 'BTechGeeks']
secondlist = ['this', 'is', 'BTechGeeks','hello']
# passing both the lists to checklist function
checkList(firstlist, secondlist)


Output:

Both lists are not equal

### Method #3:Using np.array_equal()

From our lists, we can generate two sorted numpy arrays, which we can compare using numpy.array equal() to see if they contain the same elements.

Below is the implementation:

# importing numpy
import numpy
# function to check both lists if they are equal
def checkList(firstlist, secondlist):
# Convert both lists to sorted numpy arrays and compare them to see if they're equal.
if(numpy.array_equal(numpy.array(firstlist).sort(), numpy.array(secondlist).sort())):
print("Both lists are equal")
else:
print("Both lists are not equal")

# Driver code
# given two lists
firstlist = ['hello', 'this', 'is', 'BTechGeeks']
secondlist = ['this', 'is', 'BTechGeeks','hello']
# passing both the lists to checklist function
checkList(firstlist, secondlist)


Output:

Both lists are not equal

### Method #4:Using ‘=’ Operator

The == operator can be used to compare two lists directly. If both lists are exactly identical, it will return True; otherwise, it will return False.

Below is the implementation:

# function to check if both the lists are same
def checkList(firstlist, secondlist):
# if both the lists are equal the print yes
if(firstlist == secondlist):
print("Both lists are equal")
else:
print("Both lists are not equal")

# Driver code
# given two lists
firstlist = ['hello', 'this', 'is', 'BTechGeeks']
secondlist = ['hello', 'this', 'is', 'BTechGeeks']
# passing both the lists to checklist function
checkList(firstlist, secondlist)


Output:

Both lists are not equal

### Method #5:Using reduce() +map()

We can accomplish this task of checking for equality of two lists by carefully coupling the power of map() to hash values and the utility of reduce(). This also takes into account the list’s order..

Below is the implementation:

# importing reduce from functools
from functools import reduce
# function to check if both the lists are same
def checkList(firstlist, secondlist):
# if both the lists are equal the print yes
if(reduce(lambda a, b: a and b, map(lambda x, y: x == y, firstlist, secondlist))):
print("Both lists are equal")
else:
print("Both lists are not equal")

# Driver code
# given two lists
firstlist = ['hello', 'this', 'is', 'BTechGeeks']
secondlist = ['hello', 'this', 'is', 'BTechGeeks']
# passing both the lists to checklist function
checkList(firstlist, secondlist)


Output:

Both lists are not equal