How to compare two lists in python
In this article, you will learn about how to compare two lists in python.
In the Python programming language, a list is considered as an ordered container that is mutable. You can store similar types of items in a list. In python, sometimes you need to work with more than one list and compare them. To compare two lists in python, there are some useful methods like – list.sort() and equal operator, collections.Counter(), sum(), zip() and, len(). In this article, we will explore these approaches one by one for comparing two lists in Python.
list.sort() and == operator
In this approach, lists are compared in two steps. First using the sort() method the lists are sorted and then using the double equal operator compares the list item and check if they are equal or not. Let’s see an example of it to understand things more clearly.
list1 = [10, 20, 50, 30, 40]
list2 = [10, 20, 30, 50, 40]
# sort the lists using sort()
list1.sort()
list2.sort()
if list1 == list2:
print ("Lists are Equal")
else :
print ("Lists are not Equal")
# Output: Lists are Equal
Here, you can see that we have taken two lists and the list items are the same but remain unsorted. So we have sorted them first using the sort() method and then compare them with the double equal operator and in our case, it gives output as lists are equal. In your case, if the items are not the same for both lists then it will return that lists are not equal.
collections.Counter()
In the previous approach, we need to sort both list items first. But using collections.Counter(() method we don’t need to sort them in a certain order. It checks if the elements of the two lists are the same or not. See the below code example:
import collections
list1 = [10, 20, 50, 30, 40]
list2 = [40, 20, 10, 50, 30]
# collections.Counter() is used
if collections.Counter(list1) == collections.Counter(list2):
print ("Lists are Equal")
else :
print ("Lists are not Equal")
# Output: Lists are Equal
Here, we first import collections to use collections.Counter() method and you can see that the list items are the same for both lists but they are not in order.
sum() ,zip() and len()
In this approach, the summation of both list items has been done first and then compare their length. The list items are needed to be sorted for this approach. If the list items are the same but are not sorted then it will consider it as unequal. See the below code example
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]
# sum(), zip(), len() are used
if len(list1)== len(list2) and len(list1) == sum([1 for i, j in zip(list1,list2) if i == j]):
print ("Lists are Equal")
else :
print ("Lists are not Equal")
# Output: Lists are Equal
Here, you can see that both list items are in the same order and as a result, we are getting the output as the lists are equal.
These are some useful approaches that you may follow for comparing two lists in the Python programming language. In this whole article, we have shown the output as lists are equal. Your task is to try out different list items and see if they are equal or not.