Python | Print all the common elements of two lists
Given two lists, print all the common elements of two lists.
Examples:
Input : list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 9] Output : {5} Explanation: The common element of the lists is 5. 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:
Python3
# 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 the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.
Below is the Python3 implementation of the above approach:
Python3
# 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
Method 3 : Using for loop
Python
def common_member(a, b): result = [i for i in a if i in b] return result a = [ 1 , 2 , 3 , 4 , 5 ] b = [ 5 , 6 , 7 , 8 , 9 ] print ( "The common elements in the two lists are: " ) print (common_member(a, b)) |
Output:
The common elements in the two lists are: [5]
Method 4: Using collections
This code uses the collections module to create two Counter objects from the two lists, a and b. The & operator is then used to return the common elements from the two lists. The result is then printed out.
Python3
import collections def common_member(a, b): result = collections.Counter(a) & collections.Counter(b) return result.keys() a = [ 1 , 2 , 3 , 4 , 5 ] b = [ 5 , 6 , 7 , 8 , 9 ] print ( "The common elements in the two lists are: " ) print (common_member(a, b)) |
The common elements in the two lists are: dict_keys([5])
Method 5: Using operator.countOf()
Python3
import operator as op def common_member(a, b): result = [i for i in a if op.countOf(b,i)> 0 ] return result a = [ 1 , 2 , 3 , 4 , 5 ] b = [ 5 , 6 , 7 , 8 , 9 ] print ( "The common elements in the two lists are: " ) print (common_member(a, b)) |
The common elements in the two lists are: [5]
Time Complexity: O(N)
Auxiliary Space : O(N)
Please Login to comment...