Compare Two Lists & Return Matches & Differences in Python (2 Examples)
In this tutorial, you’ll learn how to compare two lists in Python Programming Language and return the matches and differences between them. We’ll explore different methods to achieve this comparison.
The table of contents is structured as follows:
Let’s get started!
Initializing Sample Lists
Firstly, let’s initialize two sample lists:
# Initializing sample lists list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8]
This creates two lists, list1
and list2
, containing some elements.
Example 1: Finding Matches Between Lists
One way to compare two lists is to find the common elements (matches) between them. Here’s an example:
# Finding matches between lists matches = [element for element in list1 if element in list2] print(matches) # [4, 5]
In this example, we use a list comprehension to iterate over the elements of list1
and check if each element exists in list2
. The common elements between the two lists are stored in the matches
list. The output shows the elements that are present in both list1
and list2
.
Example 2: Finding Differences Between Lists
Another way to compare two lists is to find the elements that exist in one list but not in the other. Here’s how you can do it:
# Finding differences between lists differences = [element for element in list1 if element not in list2] + [element for element in list2 if element not in list1] print(differences) # [1, 2, 3, 6, 7, 8]
In this example, we use list comprehensions to iterate over the elements of list1
and list2
and check if each element is not present in the other list. The elements that are unique to each list are stored in the differences
list. The output shows the elements that are present in either list1
or list2
but not in both.
Video, Further Resources & Summary
In this tutorial, we explored different methods to compare two lists in Python. We learned how to find common and unique elements across lists. These techniques can be useful when you need to analyze the relationships between elements in different lists.
Do you need more explanations on looping through a list of integers 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.
For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:
- How to Use Lists in Python
- Loop Over List with Index in Python (2 Examples)
- Find Index of All Matching Elements in List in Python (3 Examples)
- Find Index of List Element Using Recursion in Python (2 Examples)
- Find Sublist in List in Python (2 Examples)
- Python Programming Language for Statistics & Data Science
Now you have the knowledge and techniques to compare two lists and return the matches and differences in Python. Happy coding!
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter