Intersection of two arrays in Python (lambda expression and filter function)



Examples:

 Input: arr1 [] = [1, 3, 4, 5, 7] arr2 [] = [2, 3, 5, 6] Output: Intersection: [3, 5 ] 

We have a solution to this problem, please refer to to link lambda expressions and filter () .

# Function for finding the intersection of two arrays

 

def interSection (arr1, arr2):

 

# filter (lambda x: x in arr1, arr2) - & gt;

# filter element x from the arr2 list, where x

# also lies in arr1

result = list ( filter ( lambda x: x in arr1, arr2)) 

print ( "Intersection:" , result)

 
# Driver program

if __ name__ = = "__ main__" :

arr1 = [ 1 , 3 , 4 , 5 , 7 ]

arr2 = [ 2 , 3 , 5 , 6 ]

interSection (arr1, arr2)

Output:

 Intersection: [3, 5]