Check if lists are equal in Python | Example code

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

Example Check if lists are equal in Python

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 and suggestions on this Python lists topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.