How do you find the common element between two lists?

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 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 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:




# 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

How do you find the common element between two lists?




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

Python | Intersection of two lists

Intersection of two list means we need to take all those elements which are common to both of the initial lists and store them into another list. Now there are various ways in Python, through which we can perform the Intersection of the lists.
Examples:

Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output : [9, 10, 4, 5] Input : lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] Output : [9, 11, 26, 28]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1:
This is the simplest method where we haven’t used any built-in functions.




# Python program to illustrate the intersection
# of two lists in most simple way
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
# Driver Code
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
print(intersection(lst1, lst2))

Output:

[9, 11, 26, 28]

Method 2:
This method includes the use of set() method.






# Python program to illustrate the intersection
# of two lists using set() method
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
# Driver Code
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
print(intersection(lst1, lst2))

Output:

[9, 10, 4, 5]

Method 3:
In this method we set() the larger list and then use the built-in function called intersection() to compute the intersected list. intersection() is a first-class part of set.




# Python program to illustrate the intersection
# of two lists using set() and intersection()
def Intersection(lst1, lst2):
return set(lst1).intersection(lst2)
# Driver Code
lst1 = [ 4, 9, 1, 17, 11, 26, 28, 28, 26, 66, 91]
lst2 = [9, 9, 74, 21, 45, 11, 63]
print(Intersection(lst1, lst2))

Output:

{9, 11}

Method 4:
By the use of this hybrid method the complexity of the program falls to O(n). This is an efficient way of doing the following program.




# Python program to illustrate the intersection
# of two lists
def intersection(lst1, lst2):
# Use of hybrid method
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3
# Driver Code
lst1 = [9, 9, 74, 21, 45, 11, 63]
lst2 = [4, 9, 1, 17, 11, 26, 28, 28, 26, 66, 91]
print(intersection(lst1, lst2))

Output:

[9, 9, 11]

Method 5:
This is the where the intersection is performed over sub-lists inside other lists. Here we have used the concept of filter().




# Python program to illustrate the intersection
# of two lists, sublists and use of filter()
def intersection(lst1, lst2):
lst3 = [list(filter(lambda x: x in lst1, sublist)) for sublist in lst2]
return lst3
# Driver Code
lst1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
lst2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
print(intersection(lst1, lst2))

Working: The filter part takes each sublist’s item and checks to see if it is in the source list. The list comprehension is executed for each sublist in list2.
Output:

[[13, 32], [7, 13, 28], [1, 6]]

How do you find the common element between two lists?




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

Find the common elements in two lists in Python

By Shriprakash Tiwari

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.

Common Elements Between Two Lists

Sometimes one needs to find common elements between two lists list_a and list_b. A brute force method to compare two list has O(k*l) complexity, where k and l is the length of list_a and list_b respectively. If both k and l equal to n then complexity would be O(n²). In the brute force method, every element from list_a has to be compared with list_b.

# pseudocode for brute force method
for item_a in list_a
for item_b in list_b
if item_a == item_b
commonAB add item_a

1. Using intersection() function

A simple and fairly efficient solution is to convert the first list into a set and then call the intersection() function. It returns a new set with elements common to the set with another iterable.

1
2
3
4
5
6
7
8
9
if __name__ == '__main__':
first = [1, 2, 3, 5]
second = [2, 4, 5, 7]
common = set(first).intersection(second)
print(common)# {2, 5}

DownloadRun Code

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:

{10, 20, 30}

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.