How to Compare Two Lists in Python (3 Examples)
In this tutorial, you’ll learn how to compare two lists using Python.
The content of the post looks as follows:
Let’s get right into the code!
Example Data
As a first step in this tutorial, we will create two sample lists to use as a base in the following examples.
my_list1 = ['a', 'b', 'c', 'd'] my_list2 = ['a', 'b', 'd', 'c'] print(my_list1) # ['a', 'b', 'c', 'd'] print(my_list2) # ['a', 'b', 'd', 'c']
Take a look at the previous output. We have created my_list1
and my_list2
, two lists of strings that contain the same elements, but not in the same order. Let’s see how to compare these two lists using several methods!
Example 1: Compare Two Lists With ‘==’ Operator
A simple way to compare two lists is using the ==
operator. This operator checks the equality of elements between two lists. If all elements are the same in the same order, the comparison will return “Equal”. Otherwise, it will return “Not equal”.
if my_list1 == my_list2: print("Equal") else: print("Not equal") # Not equal
As you can see, our lists are the same, but the elements aren’t in the same order, so we concluded that our lists are unequal. But what if we wanted to check if our lists have equal elements, no matter their order? If so, we can sort the lists by using the sort() method and, then, use the same code as before.
my_list1.sort() my_list2.sort() if my_list1 == my_list2: print("Equal") else: print("Not equal") # Equal
Now we were told that our lists are equal 🙂
Example 2: Compare Two Lists With set() Function
This method involves converting the lists to sets and then comparing the sets for equality. If the sets contain the same elements, regardless of their order, the comparison will return “Equal”. Otherwise, it will return “Not equal”.
if set(my_list1) == set(my_list2): print("Equal") else: print("Not equal") # Equal
Just as expected, we got that our lists are equal.
Example 3: Compare Two Lists With collections.Counter() Function
In this example, we will compare my_list1
and my_list2
using the Counter() function of the collections module. To do so, first, we will import the collections module.
import collections
Now, we can use the Counter() function to see if our two lists have equal elements.
if(collections.Counter(my_list1) == collections.Counter(my_list2)): print("Equal") else: print("Not Equal") # Equal
Here, the Counter() function creates a counter object for my_list1
, where the elements of my_list1
are the keys, and the counts of each element are the values.
Similarly, the collections.Counter(my_list2)
line creates a counter object for my_list2
. Then, the two lists are compared using the ==
operator. If they are equal, ‘Equal’ is printed. Otherwise, ‘Not equal’ would be printed.
Video, Further Resources & Summary
Do you need more explanations on how to compare the values in two lists in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Using Lists in Python (Introduction)
- Merge Sort List in Python
- Difference Between List & Set in Python
- Find Differences Between Two Columns of pandas DataFrame in Python
- Compare Two pandas DataFrames in Python
This post has shown how to compare two lists in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter