Intersection of two arrays in Python ( Lambda expression and filter function )
Last Updated :
28 Jul, 2022
Given two arrays, find their intersection. Examples:
Input: arr1[] = [1, 3, 4, 5, 7]
arr2[] = [2, 3, 5, 6]
Output: Intersection : [3, 5]
We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function.
Implementation:
Python3
def interSection(arr1,arr2):
result = list ( filter ( lambda x: x in arr1, arr2))
print ( "Intersection : " ,result)
if __name__ = = "__main__" :
arr1 = [ 1 , 3 , 4 , 5 , 7 ]
arr2 = [ 2 , 3 , 5 , 6 ]
interSection(arr1,arr2)
|
Output:
Intersection : [3, 5]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...