Report this

What is the reason for this report?

How to Compare Two Lists in Python

Updated on July 22, 2025
Safa MulaniAndrea AndersonManikandan Kurup

By Safa Mulani, Andrea Anderson and Manikandan Kurup

How to Compare Two Lists in Python

Introduction

Comparing two lists is one of the most common tasks a developer faces in Python. But what does it actually mean to “compare” them? The answer isn’t always straightforward, as the right method depends entirely on your goal. Are you trying to check if two lists are perfectly identical, down to the order of every single element? Or do you just want to know if they contain the same items, regardless of their position? Perhaps your goal is different altogether. You might need to find out what’s different between the two lists: the items that exist in one but not the other. Or maybe you want to find what they have in common.

This article will cover the primary methods for comparing two lists in Python. We’ll explore practical techniques for different comparison goals, from simple equality checks to finding unique elements. You’ll learn how to check for exact equality (content and order) using the == operator, and how to verify if lists have the same elements regardless of order with sorted() and collections.Counter. The article will also demonstrate how to find differences and similarities between lists using efficient set operations. By understanding each approach, you can choose the best solution for your specific task.

Whether you’re a beginner or an experienced programmer, you’ll find the right tool here to compare lists in Python effectively and efficiently.

Key Takeaways

  • Use the == operator for the simplest and fastest check to see if two lists are identical in both content and order.
  • For comparing list contents regardless of order, collections.Counter is the most efficient and reliable method as it correctly handles duplicate elements.
  • Leverage Python’s set operations (&, -, ^) for a highly efficient way to find common elements, differences, or unique items between two lists.
  • When comparing lists of dictionaries or other complex objects, use a list comprehension to implement custom logic based on a specific key or attribute.
  • For high-performance comparisons of large numerical lists, use the specialized and optimized functions from the NumPy library like np.array_equal().
  • Remember that == compares the contents of two lists, whereas is checks if two variables point to the exact same object in memory.
  • Always be mindful of duplicates, as methods involving set() will discard them while collections.Counter will account for their frequency.

Method 1: Checking for Exact Equality (Content and Order)

The most straightforward way to compare two lists is to check for exact equality. This is the strictest type of comparison: it returns True only if both lists contain the exact same elements in the exact same order.

For this task, you can use Python’s built-in equality operator (==).

It’s simple, fast, and perfect for when the sequence of elements is just as important as the elements themselves.


# Case 1: Identical Lists
list_a = [10, 25, 50]
list_b = [10, 25, 50]

print(f"Are list_a and list_b identical? {list_a == list_b}")

# Case 2: Same Elements, Different Order
list_c = [50, 25, 10]

print(f"Are list_a and list_c identical? {list_a == list_c}")

# Case 3: Different Elements
list_d = [10, 25, 99]

print(f"Are list_a and list_d identical? {list_a == list_d}")

Output:

Are list_a and list_b identical? True
Are list_a and list_c identical? False
Are list_a and list_d identical? False

Method 2: Checking for Content Equality (Regardless of Order)

Often, you don’t care about the order of elements; you just need to know if two lists contain the exact same items. In this case, the standard equality operator (==) won’t work. Here are two effective methods to solve this problem.

Option A: Using the sorted() Function

The most intuitive approach is to sort both lists first and then compare the results. The sorted() function returns a new sorted list without modifying the original, making it a safe and readable option.

If the sorted versions of the lists are identical, it means they contained the same elements. The following example demonstrates how to 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:

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 l2 and prints the result, and then compares l1 to l3 and prints the result.

This method is best for simplicity and readability, especially with smaller lists.

Option B: Using collections.Counter() to Compare Lists

For a more performant and robust solution, especially for large lists or lists with duplicate elements, use collections.Counter(). The Counter() class 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. This method is generally faster (O(N) time complexity) than sorting.

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 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.

Output:

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

This method is best for performance, large lists, and accurately comparing lists with duplicate items.

Method 3: Finding Differences and Similarities with set()

When your goal is to find which elements are shared or which are unique between two lists, the most efficient and Pythonic approach is to use the set() function. It creates set objects using the given lists and then compares 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.

Important Note: Converting a list to a set will remove any duplicate entries. For example, set([1, 2, 2, 3]) becomes {1, 2, 3}.

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:

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.

Finding Common Elements (Intersection)

To find elements that exist in both lists, you can find the intersection of the sets using either the & operator or the .intersection() method.

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

common_elements = set(l1) & set(l2)

print(f"Common elements: {common_elements}")

Output:

Common elements: {40, 50}

Finding Different Elements (Difference)

To find elements that are in one list but not the other, use the - operator. This operation is directional, meaning the order matters.

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

unique_to_l1 = set(l1) - set(l2)
print(f"Elements only in l1: {unique_to_l1}")

unique_to_l2 = set(l2) - set(l1)
print(f"Elements only in l2: {unique_to_l2}")

Output:

Elements only in l1: {10, 20, 30}
Elements only in l2: {80, 60, 70}

Finding All Non-Matching Elements (Symmetric Difference)

To get a combined set of all elements that are in either list, but not both, use the ^ (caret) operator or the .symmetric_difference() method.

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

diff_elements = set(l1) ^ set(l2)
print(f"Non-matching elements: {diff_elements}")

Output:

Non-matching elements: {80, 20, 70, 10, 60, 30}

Method 4: Using List Comprehension to Compare Lists

Sometimes, comparison isn’t as simple as checking for equality or set differences. You might need to compare lists of complex objects (like dictionaries) based on a specific attribute, such as an ID or a name. For these custom scenarios, list comprehensions are an elegant and powerful tool.

They provide a concise way to create new lists based on custom filtering logic. This is especially useful when set() operations can’t be used because the items (like dictionaries) are not “hashable.”

For more information about list comprehensions, refer to Understanding List Comprehensions in Python 3.

Finding Different Objects Based on a Key

Imagine you have two lists of dictionaries and you want to find which items from the first list are missing from the second, based on their 'id' key.

  • First, create a set of all the id values from the second list for fast, efficient lookups.
  • Then, use a list comprehension to iterate through the first list, keeping only the items whose id is not in our lookup set.
users_a = [
    {'id': 101, 'name': 'Alice'},
    {'id': 102, 'name': 'Bob'},
    {'id': 103, 'name': 'Charlie'}
]

users_b = [
    {'id': 101, 'name': 'Alice'},
    {'id': 103, 'name': 'Charlie'} 
]

ids_in_b = {user['id'] for user in users_b}

missing_users = [user for user in users_a if user['id'] not in ids_in_b]

print(f"Missing users: {missing_users}")

Output:

Missing users: [{'id': 102, 'name': 'Bob'}]

This approach is both readable and highly performant, as checking for an item’s existence in a set is an average O(1) operation.

List comprehensions are the ideal tool for custom comparison logic, especially when dealing with lists of dictionaries or objects where you need to compare based on a specific attribute.

Method 5: Leveraging NumPy for Numerical Lists

If you’re working with large lists of numbers, especially in data science or scientific computing, the NumPy library is your best tool. NumPy operations are written in highly optimized C code, making them significantly faster than standard Python loops for numerical tasks.

To use NumPy, you’ll need to install it first:

pip install numpy

After installation, import NumPy using:

import numpy as np

Checking for Equality

To check if two numerical lists are identical in content and order, we use np.array_equal(). It’s the high-performance equivalent of the standard == operator.

import numpy as np

array_a = np.array([10, 20, 30, 40])
array_b = np.array([10, 20, 30, 40])
array_c = np.array([40, 30, 20, 10])

print(f"Are array_a and array_b equal? {np.array_equal(array_a, array_b)}")

print(f"Are array_a and array_c equal? {np.array_equal(array_a, array_c)}")

Output:

Are array_a and array_b equal? True
Are array_a and array_c equal? False

Finding Differences and Intersections

NumPy provides optimized functions that behave like set operations but work directly on arrays.

  • np.setdiff1d(arr1, arr2): Finds elements that are in arr1 but not in arr2.
  • np.intersect1d(arr1, arr2): Finds elements that are common to both arrays.
import numpy as np

array_a = np.array([10, 20, 30, 40, 50])
array_d = np.array([40, 50, 60, 70])

difference = np.setdiff1d(array_a, array_d)
print(f"Difference (a - d): {difference}")

common_elements = np.intersect1d(array_a, array_d)
print(f"Common elements: {common_elements}")

Output:

Difference (a - d): [10 20 30]
Common elements: [40 50]

When working with large lists or arrays of numerical data where performance is critical, NumPy provides the fastest and most efficient tools for comparison. For smaller, general-purpose lists, standard Python methods are sufficient.

Which Method Should You Choose?

Choosing the right method to compare lists depends entirely on your goal. Here’s a summary to help you select the best tool for your task.

Your Goal Recommended Method(s) Key Considerations
To check if lists are identical? == operator This is the simplest, fastest check for exact equality, including element order.
To see if lists have the same items? collections.Counter The most efficient (O(N)) and reliable method. It handles duplicates correctly.
(Simple alternative) sorted() Very readable but less performant (O(N log N)) than Counter.
To find differences or similarities? set() operations (&, -, ^) Extremely fast and Pythonic. Be aware that this process removes duplicates.
To compare lists of objects/dicts? List Comprehension Offers maximum flexibility for custom logic based on specific keys or attributes.
To compare large numerical lists? NumPy (np.array_equal, np.setdiff1d) The best choice for high performance with numerical data in scientific computing.

For most day-to-day tasks, ==, collections.Counter, and set() operations will cover your needs. When faced with more complex data, turn to list comprehensions or NumPy.

Best Practices for List Comparison

To write clean, efficient, and bug-free code when comparing lists, keep these key practices in mind.

  • Clearly Define Your Goal First: Before writing any code, know exactly what you need to accomplish. Are you checking for identical order, or just the same content? Do you need to find differences or similarities? Your goal determines the correct tool.
  • Prefer Counter Over sorted() for Unordered Checks: When comparing lists regardless of order, collections.Counter is generally superior to sorted(). It’s more performant (O(N) vs. O(N log N)) and is designed to handle duplicate elements correctly and efficiently.
  • Use Sets for Membership and Difference Tests: For finding common elements, unique items, or differences, set operations are the standard. They are highly optimized and far more readable and performant than manual loops.
  • Avoid Modifying Original Lists: When performing a comparison, you should generally avoid altering your input data. Prefer functions like sorted() which return a new copy, over in-place methods like .sort() which modify the original list and can cause unintended side effects.
  • Be Mindful of Duplicates: Always remember the key difference between methods: set() operations will discard all duplicate elements, whereas collections.Counter will factor their counts into the comparison. Choose the one that matches your requirements.

Frequently Asked Questions (FAQs)

1. How do I compare two lists in Python?

The best way to compare two lists depends on your goal. Here are the three main scenarios:

  • For an exact match (content and order): Use the equality operator (==).

    [1, 2, 3] == [1, 2, 3] # True
    [1, 2, 3] == [3, 2, 1] # False
    
  • To match content (regardless of order): Use collections.Counter. This is the most efficient and reliable method.

    from collections import Counter
    Counter([1, 2, 3]) == Counter([3, 2, 1]) # True
    
  • To find differences or similarities: Convert the lists to set objects and use set operations.

2. How do I check for differences between two lists?

The most efficient way to find differences is to convert your lists to sets. This allows you to use high-performance set operations.

  • To find items in one list but not the other, use the difference operator (-).

  • To find items that are in either list but not both, use the symmetric difference operator (^).

    list_a = ['apple', 'banana', 'cherry']
    list_b = ['banana', 'date', 'fig']
    
    set_a = set(list_a)
    set_b = set(list_b)
    
    print(set_a - set_b)
    
    print(set_a ^ set_b)
    

    Output:

    {'apple', 'cherry'}
    {'apple', 'cherry', 'date', 'fig'}
    

3. What if my lists have duplicates?

If handling duplicates is important, use collections.Counter.

Using set() operations will discard all duplicates, which may not be what you want. Counter creates a count of each unique item in the list, so it accurately reflects the list’s full contents. Comparing two Counter objects checks if they have the same items with the same frequencies.

from collections import Counter

print(set([1, 2, 2]) == set([1, 2]))

print(Counter([1, 2, 2]) == Counter([1, 2]))

print(Counter([1, 2, 2]) == Counter([2, 1, 2]))

Output:

True
False
True

4. How do I find items that two lists have in common?

The most efficient way is to find the intersection of two sets. Convert both lists to sets and use the & operator.

list_a = [10, 20, 30, 40]
list_b = [30, 40, 50, 60]

common_items = set(list_a) & set(list_b)

print(common_items)

Output:

{30, 40}

5. What’s the difference between is and == when comparing lists?

  • == (Equality): Checks if two lists have the same content. This is what you should almost always use for comparison.
  • is (Identity): Checks if two variables point to the exact same object in memory. This is rarely used for list comparison unless you specifically need to check for object identity.
list_a = [1, 2, 3]
list_b = [1, 2, 3] # Same content, but a different object
list_c = list_a   # Points to the same object as list_a

print(list_a == list_b) # True (content is the same)
print(list_a is list_b) # False (they are different objects)
print(list_a is list_c) # True (they are the same object)

Conclusion

In this article, we explored the various ways to compare two lists in Python, demonstrating that the best method depends entirely on your specific goal. We covered the full spectrum of techniques: from using the simple == operator for strict, order-sensitive equality, to leveraging collections.Counter for efficient unordered comparisons. You learned how to find differences and intersections instantly with set operations, and how to tackle custom logic for complex objects using list comprehensions. Finally, we saw how NumPy provides a high-performance solution for large numerical datasets.

By understanding these distinct tools, you can now confidently choose the most effective and readable method, ensuring your code is not only correct but also perfectly suited for the task at hand.

Now that you’ve mastered comparing lists, continue building your Python skills with these helpful guides:

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 author(s)

Safa Mulani
Safa Mulani
Author
Andrea Anderson
Andrea Anderson
Editor
Technical Editor
See author profile
Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Category:
Tags:

Still looking for an answer?

Was this helpful?

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

- Alex

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

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.