Compare Two Lists of Integers in Python (5 Examples)

 

In this Python tutorial you’ll learn how to compare two lists of integers.

The tutorial will contain these content blocks:

Let’s get started!

 

Creating Example Data

At the start, we have to create some example data:

list1 = [1, 2, 3, 4, 5] # create first sample list
list2 = [5, 4, 3, 2, 1] # create second sample list
list3 = [4, 5] # create third sample list

The previous output of the Python console illustrates that we will use three integer lists as sample data. Now, let’s see the first example of comparing two of these lists.

 

Example 1: Check if Two Lists are Exactly Same

The following Python code shows how to check if two lists are identical.

if list1 == list2: # test if identical
    print("The lists are identical.")
else:
    print("The lists are not identical.")
# The lists are not identical.

As noticed, The lists are not identical is returned after testing the equality between list1 and list2 using the if-else statements.

One might also be interested in checking if the lists contain the same elements yet in different order. See how it is implemented next!

 

Example 2: Check if Two Lists Have Same Elemement

In this example, we use both if-else statements and the sorted() function to sort lists to be compared before the comparison.

if sorted(list1) == sorted(list2): # test if contain same elements
    print("The lists have the same elements.")
else:
    print("The lists don't have the same elements.")
# The lists have the same elements.

This comparison checks if the sorted versions of list1 and list2 are the same. If they are the same after sorting, then the original lists contain the same elements (though not necessarily in the same order).

 

Example 3: Check Common Elements between Two Lists

Example 3 explains how to find common elements across list2 and list3 using the set() function.

common_elements = set(list1) & set(list2) # find common elements
print(f"Common elements: {common_elements}")
# Common elements: {1, 2, 3, 4, 5}

Both set(list1) and set(list2) convert the lists list1 and list2 into sets, which are unordered collections of unique elements. Then, the & operator returns the intersection of the two sets.

 

Example 4: Check Unique Elements between Two Lists

In Example 4, I’ll explain how to find differentiating elements in list2 from list3 and vice versa.

Again, here, we use the set() function to obtain unique sets of elements from list2 and list3. Then, we use the - operator to find differences between these sets. Lastly, we print the results using fstrings like in previous case.

only_in_list2 = set(list2) - set(list3) # find unique elements
only_in_list3 = set(list3) - set(list2)
 
print(f"Elements only in list2: {only_in_list2}") 
print(f"Elements only in list3: {only_in_list3}")
# Elements only in list2: {1, 2, 3}
# Elements only in list3: set()

As seen, the uncommon elements for both lists are printed. For list3, an empty set is obtained since there are no unique elements in list3 compared to list2. In other words, list3 is a subset of list2.

If you are interested in a more direct way to test if a list is a subset of the other, see the next example.

 

Example 5: Check if One List is Subset of Another

In this example, I’ll show how to check if a list subsumes another list using the issubset() method.

if set(list3).issubset(list2): # test if one is subset
    print("List3 is a subset of list2.")
else:
    print("List3 is not a subset of list2.")
# List3 is a subset of list2.

Above, you can see how the selected string is printed based on which condition (subset or not) holds. Here List3 is a subset of list2. is printed as expected.

 

Video, Further Resources & Summary

Some time ago, I have released a video on my YouTube channel, which shows the content of this tutorial. You can find the video below.

 

The YouTube video will be added soon.

 

Also, you may want to have a look at the other tutorials on Statistics Globe:

 

In this Python programming tutorial, you have learned how to analyze dissimilarities and similarities between two integer lists. Don’t hesitate to let me know in the comments section if you have any further questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top