Check if two Lists are Equal
Python - Check if Two Lists are Equal
To check if two lists are equal in Python, use Equal to comparison operator. If two lists are equal, then equal to operator returns True, else it returns False.
Examples
1. Check if lists: list1 and list2 are equal
In the following program, we take two lists: list1
and list2
with some numeric values. We shall then use Equal-to operator to check if these two lists are equal. We shall use the expression using Equal-to operator as a condition in the if else statement.
Python Program
list1 = [12, 8, 4, 6]
list2 = [12, 8, 4, 6]
if list1 == list2:
print('list1 and list2 are equal.')
else:
print('list1 and list2 are not equal.')
Output
list1 and list2 are equal.
2. Check if lists: list1 and list2 are equal, given elements are not same
Now, let us take the lists such that the second element is not equal, and see what the equal to operator returns.
Python Program
list1 = [12, 8, 4, 6]
list2 = [12, 7, 4, 6]
if list1 == list2:
print('list1 and list2 are equal.')
else:
print('list1 and list2 are not equal.')
Output
list1 and list2 are not equal.
Summary
In this tutorial of Python Examples, we learned how to check if two lists are equal, using equal to operator.