How to Compare Two Lists in Python

Introduction

When programming in, or learning, Python you might need to determine whether two or more lists are equal. When you compare lists for equality, you’re checking whether the lists are the same length and whether each item in the list is equal. Lists of different lengths are never equal.

This article describes how to use the following Python features to compare lists:

  • sort() method or the sorted() function with the == operator
  • set() function with the == operator
  • reduce() and map() functions with the == operator
  • collections.Counter() class with the == operator
  • List comprehension

Using the sort() Method or the sorted() Function to Compare Two Lists in Python

You can use the sort() method or the sorted() function to sort lists with the purpose of comparing them for equality. The sort() method sorts the list in place, while the sorted() function returns a new list. After sorting, lists that are equal will have the same items in the same index positions. The == operator compares the lists, item by item (element-wise comparison).

The order of the original list items is not important, because the lists are sorted before comparison.

Note: You can sort only lists with items of the same data type.

sort() Method Example


l1 = [10, 20, 30, 40, 50] 
l2 = [20, 30, 50, 40, 70] 
l3 = [50, 10, 30, 20, 40] 

l1.sort() 
l2.sort() 
l3.sort() 

if l1 == l2: 
    print ("The lists l1 and l2 are the same") 
else: 
    print ("The lists l1 and l2 are not the same") 

if l1 == l3: 
    print ("The lists l1 and l3 are the same") 
else: 
    print ("The lists l1 and l3 are not the same") 
  

Output:


The lists l1 and l3 are the same
The lists l1 and l2 are not the same
  

sorted() Function Example


l1 = [10, 20, 30, 40, 50]
l2 = [20, 30, 50, 40, 70]
l3 = [50, 10, 30, 20, 40]

l1_sorted = sorted(l1)
l2_sorted = sorted(l2)
l3_sorted = sorted(l3)

if l1_sorted == l2_sorted:
    print ("The lists l1 and l2 are the same")
else:
    print ("The lists l1 and l2 are not the same")

if l1_sorted == l3_sorted:
    print ("The lists l1 and l3 are the same")
else:
    print ("The lists l1 and l3 are not the same")
  

Output:


The lists l1 and l3 are the same
The lists l1 and l2 are not the same
  

Using the reduce() and map() Functions to Compare Two Lists in Python

You can use the Python map() function along with the functools.reduce() function to compare the data items of two lists. When you use them in combination, the map() function applies the given function to every element, and the reduce() function ensures that it applies the function in a consecutive manner.

Note: The order of the list items is important when using reduce() and map(). If required, sort the lists first.


import functools

l1 = [10, 20, 30, 40, 50]
l2 = [20, 30, 50, 40, 70]
l3 = [10, 20, 30, 40, 50]

if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q, l1, l2), True):
    print ("The lists l1 and l2 are the same")
else:
    print ("The lists l1 and l2 are not the same")

if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q, l1, l3), True):
    print ("The lists l1 and l3 are the same")
else:
    print ("The lists l1 and l3 are not the same")
  

Output:


The lists l1 and l2 are not the same
The lists l1 and l3 are the same
  

Using the set() Function to Compare Two Lists in Python

You can use the set() function to create set objects using the given lists and then compare the sets for equality using the == operator.

Note: Duplicate list items appear only once in a set.


l1 = [10, 20, 30, 40, 50]
l2 = [50, 10, 30, 20, 40]

a = set(l1)
b = set(l2)

if a == b:
    print("Lists l1 and l2 are equal")
else:
    print("Lists l1 and l2 are not equal")
  

Output:

Conclusion

This article described a few different ways to compare lists for equality in Python. Continue your learning with more Python tutorials.