Skip to content

Check if lists are equal in Python | Example code

  • by

The simplest way to check to lists are equal or not is to use the sort() function and equal operators in Python.

Note: that the == operator only checks if the values in the two lists are equal, not if they are the same object in memory.

Example Check if lists are equal in Python

A simple example code compares two lists and finds out if they are identical meaning having the same elements or not.

Using list.sort() and == operator

listA = ['Mon', 'Tue', 'Wed', 'Thu']
listB = ['Mon', 'Wed', 'Tue', 'Thu']

# Sort the lists
listA.sort()
listB.sort()

# Check for equality
if listA == listB:
    print("Lists are equal")
else:
    print("Lists are not equal")

Output:

Check if lists are equal in Python

Do comment if you have any doubts or suggestions on this Python lists topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading