Python | Intersection of two lists

Python Methods and Functions

The intersection of two lists means that we need to take all those elements that are common to both initial lists and store them in another list. Now in Python there are various ways in which we can perform list intersection. 
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] 

Method 1:
This is the simplest method, where we haven't used any built-in functionality.

# Python program to illustrate intersection
# of two lists in the simplest 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 involves using the set () method.

# Prog Python frame to illustrate intersection
# of two lists using the set () method

def intersection (lst1, lst2):

return list ( set (lst1) & amp;  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 interscetion () function to calculate the overlapping list.  Intersection () is the first class part of the set.

# Python program to illustrate intersection
# from 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))

Exit :

 {9, 11} 

Method 4:
When using this hybrid method , the program's complexity drops to O (n). This is an efficient way to do the following program.

# Python program to illustrate intersection
# of two lists

def intersection (lst1, lst2):

 

# Using a 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 where intersection occurs over sublists within other lists. Here we have used the concept of filter ().

# Python program to illustrate intersection
# from two lists, sublists and using the 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))

Work: The filter part takes an item from each sublist and checks if it is in the original list. List comprehension is performed for each sublist in list2. 
Output:

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




Tutorials