Compare Two Lists Using for Loop in Python (Example)
In this tutorial, I’ll demonstrate how to compare lists using for loops in Python.
The post contains the following topics:
Let’s dive into it!
Creation of Example Data
Before all else, let’s create some example data. I will create two sample lists for demonstration.
list1 = [1, 2, 3, 4, 7] # create first sample list list2 = [1, 2, 3, 4, 5, 5] # create second sample list
As seen above, we have list1
, which has 5 numbers, and list2
, which has 6 numbers, including a duplicate. Let’s now compare them by iterating through the elements of both lists!
Example: Compare Lists Using for-loop
In this example, I’ll demonstrate how to use if-else statements and for-loops to iterate through the elements of list1
and list2
to compare their items.
First, we need to convert the lists into sets to get the unique elements of each list. See the code below.
set1 = set(list1) # find unique elements of list1 print(set1) # print set1 # {1, 2, 3, 4, 7} set2 = set(list2) # find unique elements of list2 print(set2) # print set2 # {1, 2, 3, 4, 5}
Now the unique elements of list1
are stored in set1
and the unique elements of list2
are stored in set2
, as shown above.
In the next step, we will check if the sets are equal. If so, the code will return the “Both lists have the same unique elements!” statement.
If not, we will iterate through both list1
and list2
in changing orders. If an element is in one of the lists but not in the other, the code will return the difference using an fstring. Follow the script below.
if set1 == set2: # compare sets print("Both lists have the same unique elements!") else: for item in set1: # for loop iterating through sets if item not in set2: print(f"Item {item} from list1 is not in list2.") for item in set2: # for loop iterating through sets if item not in set1: print(f"Item {item} from list2 is not in list1.") # Item 7 from list1 is not in list2. # Item 5 from list2 is not in list1.
As desired, the computation has returned the differentiating element in list1
and list2
separately.
If the lists included the same elements, Both lists have the same unique elements!
would be returned without iteration.
Video, Further Resources & Summary
Do you need further information on the Python code of this page? Then, I can recommend having a look at the following video on my YouTube channel. In the video, I’m illustrating the topics of this article:
The YouTube video will be added soon.
Furthermore, you might read the related articles on this website. I have published several tutorials already.
- Compare Each Element of Two Lists in Python
- How to Compare Two Lists in Python
- Compare Two Lists & Return Matches & Differences in Python
- Introduction to Python
Summary: You have learned in this tutorial how to find similarities and dissimilarities in lists via for loop in Python programming. Let me know in the comments section below, if you have additional questions.
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.