Thursday, September 25, 2025
HomeLanguagesIntersection of two arrays in Python ( Lambda expression and filter function...

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

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




# Function to find intersection of two arrays
 
def interSection(arr1,arr2):
 
    # filter(lambda x: x in arr1, arr2) -->
    # filter element x from list arr2 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]
RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6754 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS