Tutorial

How to Compare Two Lists in Python

Updated on January 12, 2023
authorauthor

Safa Mulani and Andrea Anderson

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
  • collection.Counter() class with the == operator
  • list comprehension

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

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

The following example demonstrates how to use the sort() method to sort and compare lists for equality:

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") 

The output is:

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

The preceding example code sorts each list, compares l1 to l3 and prints the result, and then compares l1 to l2 and prints the result.

sorted() Function Example

The following example demonstrates how use the sorted() function to sort and compare lists for equality:

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")

The output is:

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

The preceding example code returns a sorted version of each list, compares l1 to l3 and prints the result, and then compares l1 to l2 and prints the result.

Using the reduce() and map() Functions to Compare Lists

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.

The map() function accepts a function and an iterable as arguments. The map() function applies the given function to each item of the iterable and then returns a map object (iterator) as the result.

The functools.reduce() function also accepts a function and an iterable as arguments. The functools.reduce() function applies the given function to every element of the iterable recursively. Initially, functools.reduce() applies the function on the first and the second items and returns the result, and then applies the function on the result and the third item, and continues until the list has no items left.

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.

The order of the list items is important when you use the reduce() and map() functions. Lists with the same items in different order will not return true when compared for equality. If required, you can sort the lists first.

The following example demonstrates how to use the reduce() and map() functions to compare lists for equality:

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")

The output is:

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

The preceding example code compares l1 to l2 and then compares l1 to l3.

Using the set() Function to Compare Lists

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

The order of the original list items is not important, because the == operator returns true when each set contains identical items in any order.

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

The following example demonstrates how to create sets from lists and compare the sets for equality:

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")

The output is:

Output
Lists l1 and l2 are equal

The preceding example code creates sets a and b from lists l1 and l2 and then compares the sets and prints the result.

Using the collections.Counter() Class to Compare Lists

The collections.Counter() class can be used to compare lists. The counter() function counts the frequency of the items in a list and stores the data as a dictionary object in the format value:frequency. If two lists have the same dictionary output, you can infer that the lists are the same.

The order of the original list items isn’t important when you use the Counter class to compare lists.

The following example demonstrates how to create Counter objects from the given lists and compare them for equality:

import collections

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

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

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

The output is:

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

The preceding example code creates Counter objects for lists l1 and l2, compares them, and prints the result. The code repeats for lists l1 and l3.

Using List Comprehension to Compare Lists

You can use list comprehension to compare two lists. For more information about list comprehensions, refer to Understanding List Comprehensions in Python 3.

The order of the original list items isn’t important when you use list comprehension to compare lists.

The following example demonstrates how to use a list comprehension to compare lists:

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

res = [x for x in l1 + l2 if x not in l1 or x not in l2]

print(res)
if not res:
    print("Lists l1 and l2 are equal")
else:
    print("Lists l1 and l2 are not equal")

res2 = [x for x in l1 + l3 if x not in l1 or x not in l3]

print(res2)
if not res2:
    print("Lists l1 and l3 are equal")
else:
    print("Lists l1 and l3 are not equal")

The preceding example code sets a pointer element x to the lists l1 and l2, then checks if the item pointed by the pointer element is present in the lists. If the result, res is an empty list, then you can infer that the lists are equal, since there are no items that appear in only one of the lists.

The output is:

Output
[10, 75] Lists l1 and l2 are not equal

Conclusion

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author


Default avatar

Technical Editor


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 16, 2020

Why “reduce()”? Maybe “all()”?

- Alex

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.