
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Two Lists Have At Least One Common Element in Python
To check if two lists have at least one common element, we will loop through list1 and list2 and check for atleast one common element in both the lists. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type.
Let's say we have the following inputs i.e. 2 lists ?
[15, 45, 67, 30, 98] [10, 8, 45, 48, 30]
The output should be the following i.e. two lists have atleast one common element ?
Common Element Found
Check if two lists have at least one common element using for loop
We have used nested for loops to check if the two lists entered by the user is having at least two elements or not. Both the lists are traversed and the common element is found using the if statement. The input() method is used for user input ?
Example
def commonFunc(A, B): c = "Common Element Not Found" # traverse in the 1st list for i in A: # traverse in the 2nd list for j in B: # if one common if i == j: c="Common Element Found" return c return c # Driver code A=list() B=list() n1=int(input("Enter the size of the first List =")) print("Enter the Element of the first List =") for i in range(int(n1)): k=int(input("")) A.append(k) n2=int(input("Enter the size of the second List =")) print("Enter the Element of the second List =") for i in range(int(n2)): k=int(input("")) B.append(k) print("Display Result =",commonFunc(A, B))
Output
Enter the size of the first List =5 Enter the Element of the first List = 15 45 67 30 98 Enter the size of the second List =7 Enter the Element of the second List = 10 8 45 48 30 86 67 Display Result = Common Element Found Enter the size of the first List =5 Enter the Element of the first List = 10 20 30 40 50 Enter the size of the second List =5 Enter the Element of the second List= 23 39 45 67 89 Display Result = Common Element Not Found
Check if two lists have at least one common element using set_intersection()
We have used the set_intersection() method to check if two have lists have at least one common element. First, the lists is converted to set and then the method is applied to find the common elements ?
Example
def commonFunc(list1, list2): p_set = set(p) q_set = set(q) if len(p_set.intersection(q_set)) > 0: return True return False # First List p = [4, 10, 15, 20, 25, 30] print("List1 = ",p) # Second List q = [12, 24, 25, 35, 45, 65] print("List2 = ",q) print("Are common elements in both the lists? ",commonFunc(p, q))
Output
List1 = [4, 10, 15, 20, 25, 30] List2 = [12, 24, 25, 35, 45, 65] Are common elements in both the lists? True
Check if two lists have at least one common element using Set
In this example, we have used the set() method to convert the lists to set and then the & operator is used to find the common elements. Therefore, we haven't used the set_intersection() method here ?
Example
def commonFunc(list1, list2): p_set = set(p) q_set = set(q) if (p_set & q_set): return True else: return False # First List p = [5, 10, 15, 20, 25, 30] print("List1 = ",p) # Second List q = [12, 20, 30, 35, 40, 50] print("List2 = ",q) print("Are common elements in both the lists? ",commonFunc(p, q))
Output
List1 = [5, 10, 15, 20, 25, 30] List2 = [12, 20, 30, 35, 40, 50] Are common elements in both the lists? True
Above, we saw that True is returned, since both the lists had at least one common element. In this example, the common element was 30.