Given two lists, print all the common element of two lists.
Examples:
Input : list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common elements of
both the lists are 3 and 4
Input : list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
Output : No common elements
Explanation: They do not have any
elements in common in between them
Method 1:Using Set’s & property
Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
Below is the Python3 implementation of the above approach:
# Python program to find the common elements # in two lists def common_member(a, b): a_set = set(a) b_set = set(b) if (a_set & b_set): print(a_set & b_set) else: print("No common elements") a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] common_member(a, b) a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9] common_member(a, b) |
Output:
{5}
No common elements
Method 2:Using Set’s intersection property
Convert the list to set by conversion. Use intersection function to check if both sets have any elements in common. If they have any elements in common, then print the intersection of both the sets.
Below is the Python3 implementation of the above approach:
# Python program to find common elements in # both sets using intersection function in # sets # function def common_member(a, b): a_set = set(a) b_set = set(b) # check length if len(a_set.intersection(b_set)) > 0: return(a_set.intersection(b_set)) else: return("no common elements") a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] print(common_member(a, b)) a =[1, 2, 3, 4, 5] b =[6, 7, 8, 9] print(common_member(a, b)) |
Output:
{5}
No common elements
Recommended Posts:
- Python | Count of common elements in the lists
- Python | Find common elements in list of lists
- Python program to find common elements in three lists using sets
- Python | Print the common elements in all sublists
- Python | Merge List with common elements in a List of Lists
- Python | Check if two lists have at-least one element common
- Python | Check if two lists have any element in common
- Print lists in Python (4 Different Ways)
- Python code to print common characters of two Strings in alphabetical order
- Python | Common Row elements Summation
- Python - Test Common Elements Order
- Python | Split the Even and Odd elements into two different lists
- Python - Common list elements and dictionary values
- intersection_update() in Python to find common elements in n arrays
- Python - Pair lists elements to Dictionary
- Python | Uncommon elements in Lists of List
- Python | Get positive elements from given list of lists
- Python - Filter odd elements from value lists in dictionary
- Python - Elements with K lists similar index value
- Python - Elements frequency count in multiple lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

