By Safa Mulani, Andrea Anderson and Manikandan Kurup
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.
==
operator for the simplest and fastest check to see if two lists are identical in both content and order.collections.Counter
is the most efficient and reliable method as it correctly handles duplicate elements.set
operations (&
, -
, ^
) for a highly efficient way to find common elements, differences, or unique items between two lists.np.array_equal()
.==
compares the contents of two lists, whereas is
checks if two variables point to the exact same object in memory.set()
will discard them while collections.Counter
will account for their frequency.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
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.
sorted()
FunctionThe 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.
collections.Counter()
to Compare ListsFor 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.
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.
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}
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}
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}
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.
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.
set
of all the id
values from the second list for fast, efficient lookups.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.
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
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
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.
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.
To write clean, efficient, and bug-free code when comparing lists, keep these key practices in mind.
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.sorted()
which return a new copy, over in-place methods like .sort()
which modify the original list and can cause unintended side effects.set()
operations will discard all duplicate elements, whereas collections.Counter
will factor their counts into the comparison. Choose the one that matches your requirements.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.
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'}
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
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}
==
(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)
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:
6 Ways to Concatenate Lists in Python
Learn multiple techniques to join lists together, including using the +
operator, extend()
, unpacking, and more, so you can efficiently combine data in your Python programs.
Python Compare Strings - Methods & Best Practices
Discover the best ways to compare strings in Python, including case sensitivity, locale-aware comparisons, and tips for avoiding common pitfalls.
Understanding Lists in Python 3
Gain a solid foundation in Python lists, covering their properties, how to create and manipulate them, and best practices for working with list data structures.
Python Find String in List: Complete Guide with Examples
Learn practical methods to search for strings within lists, including using loops, list comprehensions, and built-in functions, with clear examples for real-world scenarios.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.