Find Common Elements in Three Lists in Python (3 Examples)
Hi! This tutorial will show you how to extract the common elements in 3 lists in the Python programming language.
Here is an overview:
Let’s get coding!
Create 3 Example Lists of Integers
We will, here, create 3 examples of Python lists of integers from which we will get the common elements. So, in your favorite Python programming IDE, run the lines of code below to create the example lists of integers:
list1 = [1, 2, 3, 4, 5] list2 = [3, 4, 5, 6, 7] list3 = [5, 6, 7, 4, 9] print(list1) print(list2) print(list3) # [1, 2, 3, 4, 5] # [3, 4, 5, 6, 7] # [5, 6, 7, 4, 9] print(type(list1)) print(type(list2)) print(type(list3)) # <class 'list'> # <class 'list'> # <class 'list'>
Now that we have our lists created let us now examine ways to return the common elements in the lists.
Example 1: Extract Common Elements in Lists Using set() Function & intersection() Method
common_elements = set(list1).intersection(list2).intersection(list3) print(list(common_elements)) # [4, 5]
In the above example, we converted the first list to a set, then we chained the
intersection() method, with list2 and list3 as its argument, to find the elements common in all 3 lists.
By default, the result is returned as a set of common elements. However, we used the list() function to convert it to a list in
Note that there is no hard and fast rule about doing this; it depends entirely on your use case. However, we are converting to a list in this tutorial simply for the sake of uniformity of the results in each example.
Example 2: Extract Common Elements in Lists Using set() Function and & Operator
In this next example, we will use the
set() function and “&” bitwise operator to return the common elements in the 3 lists:
common_elements = set(list1) & set(list2) & set(list3) print(list(common_elements)) # [4, 5]
Here, we convert each of the three lists to a set using the
set() function and then use the “&” operator to find the intersection of the three sets. The resulting set contains the common elements in the three lists.
Again, as in the previous example, we convert the resulting set to a list using the
Example 3: Extract Common Elements in Lists Using for Loop
In this last example, we will use a for loop to find the common elements in the lists:
common_elements =  for i in list1: if i in list2 and i in list3: common_elements.append(i) print(common_elements) # [4, 5]
In this example, we loop through the elements of list1, and check if each element is also present in list2 and list3 using the in operator. If an element is present in all three lists, we append it to the “common_elements” list. Lastly, we print the “common_elements” list.
With that, we have demonstrated 3 simple ways to find the common elements in 3 lists in Python. I do hope you found this tutorial beneficial!
Video, Further Resources & Summary
Do you need more explanations on how to find the common elements in 3 lists in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to find the common elements in 3 lists in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Change Index of Element in List in Python (Example)
- Difference Between List & Set in Python
- count() Method for Lists in Python (2 Examples)
- Append to 2D List in Python (3 Examples)
- Learn Python Programming
This post has shown how to find the common elements in 3 lists in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.