How to compare two lists in Python?

PythonServer Side ProgrammingProgramming

The list in python is a collection of similar items. We may at times need to compare data items in the two lists to perform certain operations. We will discuss certain methods to compare two lists in python.

Using list.sort() and == operator

The list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list. This means that the list [1,2,3] will be equal to the list [2,1,3] according to this method of comparison.

Example

 Live Demo

def compareList(l1,l2):
   l1.sort()
   l2.sort()
   if(l1==l2):
      return "Equal"
   else:
      return "Non equal"
l1=[1,2,3]
l2=[2,1,3]
print("First comparison",compareList(l1,l2))
l3=[1,2,3]
l4=[1,2,4]
print("Second comparison",compareList(l3,l4))

Output

First comparison Equal
Second comparison Non equal

Using collections.Counter()

This method tests for the equality of the lists by comparing frequency of each element in first list with the second list. This method also does not take into account the order of the elements of the list.

Example

 Live Demo

import collections
def compareList(l1,l2):
   if(collections.Counter(l1)==collections.Counter(l2)):
      return "Equal"
   else:
      return "Non equal"
l1=[1,2,3]
l2=[2,1,3]
print("First comparison",compareList(l1,l2))
l3=[1,2,3]
l4=[1,2,4]
print("Second comparison",compareList(l3,l4))

Output

First comparison Non equal
Second comparison Equal

Using sum() ,zip() and len()

This method first compares each element of the two lists and store those as summation of 1, which is then compared with the length of the other list. For this method, we have to first check if the lengths of both the lists are equal before performing this computation.

This method also checks for the order of the elements. This means that the list [1,2,3] is not equal to the list [2,1,3].

Example

 Live Demo

def compareList(l1,l2):
   if(len(l1)==len(l2) and len(l1)==sum([1 for i,j in zip(l1,l2) if i==j])):
      return "Equal"
   else:
      return "Non equal"
l1=[1,2,3]
l2=[2,1,3]
print("First comparison",compareList(l1,l2))
l3=[1,2,3]
l4=[1,2,3]
print("Second comparison",compareList(l3,l4))

Output

First comparison Non equal
Second comparison Equal

As in the above example, for the first comparison, the data items in both the lists are equal but the order of the elements is different. Therefore, the first comparison returns not equal.

Using == operator

This is a modification of the first method. In this method, the lists are compared without sorting and thus, this method takes into account the order of the data items in the lists.

Example

 Live Demo

def compareList(l1,l2):
   if(l1==l2):
      return "Equal"
   else:
      return "Non equal"
l1=[1,2,3]
l2=[2,1,3]
print("First comparison",compareList(l1,l2))
l3=[1,2,3]
l4=[1,2,3]
print("Second comparison",compareList(l3,l4))

Output

First comparison Non equal
Second comparison Equal

These are some of the methods to compare lists in python, both taking into account the order of the data items as well as without taking into account the order of data items.

raja
Published on 10-Mar-2021 14:02:16
Advertisements