💡 Outline
You can first convert first list into set usingset()
and callintersection()
by passing second list as parameter to find common element in two lists in Python.
123 common_list = set(list_one).intersection(list_two)
In this tutorial, we will see different methods to find common elements in two lists in python.
Using the intersection()
Function
This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection()
function is a built-in python function that is used to return a set that contains the elements which are common in two sets. The sets can be of any form i.e a list or a dictionary.
Example:
1 2 3 4 5 6 7 |
if __name__ == '__main__': list_one = [5, 10, 15, 20, 25, 30] list_two = [10, 20, 30, 40, 50, 60] common_list = set(list_one).intersection(list_two) print(common_list) |
Output:
Note that,
__name__
variable is used in the above code. It is a built-in variable that is used to see whether the current code is being run on its own or an external module is being imported from somewhere. Thus, the__name__
variable checks the name of the current module in the ongoing code. It is usually used with an if statement and where it is assigned to'__main__'
when there is no module in the program. Otherwise,__name__
is assigned to the name of the module used in the program.
Using List Comprehension
List Comprehension is a very concise way which helps in creating a list based on values stored from existing list or lists. This is also one way of finding common elements from two lists.
Example:
1 2 3 4 5 6 7 |
if __name__ == '__main__': list_one = [5, 10, 15, 20, 25, 30] list_two = [10, 20, 30, 40, 50, 60] common_list = [c for c in list_one if c in list_two] print(common_list) |
Output:
NOTE: There is one drawback in using this method to find common elements i.e this method should not be used in the case of large lists.
Using &
Operator
The set’s &
operator is also one way of finding common elements in two lists. In this method, both the lists are converted into sets first and then the common elements are found using &
.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def common_element(a, b): list_one = set(a) list_two = set(b) if (list_one & list_two): print(list_one & list_two) else: print("There are no common elements") a = [5, 10, 15, 20, 25, 30] b = [10, 20, 30, 40, 50, 60] common_element(a, b) a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 10] common_element(a, b) |
Output:
There are no common elements
Explantion
- First both the lists are converted into set using
set()
function. - Used
&
to find common elements in two sets in Python. - We have used
def
keyword in above code. def` is used to define a function with more than one statement in a function. This function can be called with the help of the function name at the end of the code. In the above code, we have called the function twice at the end where we made two cases of the sets to find the common elements, one with some common elements and one with no common elements.
That’s all about how to find common elements in two lists in Python.