Tutorials  /  Python

How to Compare Two Lists in Python

Ccentron Redaktion · December 2024 ·3 min read ·Python, Tutorial

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
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

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

YAML

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:

Code

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

sorted() Function Example

YAML

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:

Code

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.

Python

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:

Code

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.

Python

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:

Code

Lists l1 and l2 are equal
  

Conclusion

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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren