Find the common elements in two lists in Python
In this tutorial, We are going to learn how to find the common elements in two lists in Python.
To find the common list from two lists, we have to store the common elements in the third variable. There are various methods to find and store the common elements from two lists.
How to find the common elements in two lists in Python
To learn the various ways to find the common elements from two lists in Python. We have to make two separate lists.
We will learn all the ways with an example.
Using a function
Example 1: Make a function for both lists. If there are common elements in both the list, then it will return common elements in list c.
If both lists do not contain any common elements then it will return an empty list.
a=[2,3,4,5] b=[3,5,7,9] def common(a,b): c = [value for value in a if value in b] return c d=common(a,b) print(d)
Run this code online
Both the list have common elements 3 & 5, So it will return [3,5].
[3, 5]
Using set() function
Example 2: Using set( ) function, We can return common elements of a list in the third variable. If both lists do not contain any common elements then it will return an empty list. You may learn more: Sets and its Methods in Python
a=[2,9,4,5] b=[3,5,7,9] def common(lst1, lst2): return list(set(lst1) & set(lst2)) e=common(a,b) print(e)
Run this program
Both the list have common elements 9 & 5, So it will return [9,5].
Output–
[9, 5]
Find the common items from two lists using set().intersection()
Example 3- Using set( ).intersection(), We can print the list of common elements of a list, but it can not store in any of the variables.
If both lists do not contain any common elements then it will return an empty set( ).
a=[2,9,4,5] b=[3,5,7,2] print(set(a).intersection(b))
Run this program online
Both the list have common elements 2 & 5, So it will return [2,5].
Output–
{2, 5}
the problem with set method is
if l1=[2,3,2,4] and l2=[2,2,2,7]
conveerting these two to sets and tai
king interstion will give {2} when infact the elements common for the lists are [2,2]
#You can use this method right
l1=[2,3,2,4]
l2=[2,2,2,7]
c = [value for value in l1 if value in l2]
print(c)
How can we find non common elements from the two sets?
Its better if you apply a nested loop and consider the conditional statement as:
if str1[i]!= str2[j]:
non_common.append(str1[i])
non_common.append(str2[j])
where Non_common will be a list.. Hope thatwould work!
Description
Let’s say you have two lists A and B. Identify the elements which are common in the two lists A and B and return them in a sorted manner. For example
Sample Input :
A = [5, 1, 3, 4, 4, 5, 6, 7]
B = [3, 3, 5, 5, 1 ,7 ,2]
l1=[8, 13, 21, 34, 55, 89,1, 1, 2, 3, 5]
l2=[ 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7,]
c = list(set([value for value in l1 if value in l2]))
print(c)
1 Write the program to find the lists consist of at least one common element.
Testcase1:
Input:
Enter list1 elements :1 2 3 4 5 6
Enter list2 elements : 7 8 9 2 10
Output:
The common element is: 2