Python: Check if Two Lists are Equal | How do you Check if a List is the Same as Another List Python?

In Order to check if two lists or identical or not we need to check if two unordered lists have similar elements in the exact similar position.  You might need this in your day-to-day programming. Go through the tutorial over here and determine if two lists are equal or not in Python easily.

We have mentioned the various techniques for finding if a list is the same as the Another or Not along with sample programs. Use them as a guide for resolving your doubts if any on Comparing Two Lists and finding whether they are identical or not. You can also find difference between two lists python by going through our other tutorials.

List – Definition

Multiple items may be stored in a single variable using lists. Square brackets[] are used to create a list. 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.

Examples for Checking if Two Lists are Equal or Not

Comparing lists irrespective of the 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 the 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

How to Compare if Two Lists are Identical or Not?

There are several ways to check if two lists are equal or not. We have outlined some of them so that you can choose the method that you are comfortable with and determine if the two lists are equal or not. They are explained in the following way

  • Using sorting()
  • Using Counter() function
  • Using np.array_equal()
  • Using ‘=’ Operator
  • Using reduce() +map()

Check lists irrespective of order of elements

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)

Python Program to check if two lists are equal or not using Sorting()

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)

Comparison of two lists using counter() function in Python

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

Checking lists based on of order of elements

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)

Comparison of two lists using = Operator in Python

Output:

Both lists are not equal

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

We can accomplish this task of checking for the 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