w3resource

Python: Find intersection of two given arrays using Lambda

Python Lambda: Exercise-11 with Solution

Write a Python program to find the intersection of two given arrays using Lambda.

Sample Solution:

Python Code :

# Define two lists 'array_nums1' and 'array_nums2' containing integers
array_nums1 = [1, 2, 3, 5, 7, 8, 9, 10]
array_nums2 = [1, 2, 4, 8, 9]

# Display a message indicating that the following output will show the original arrays
print("Original arrays:")
print(array_nums1)  # Print the contents of 'array_nums1'
print(array_nums2)  # Print the contents of 'array_nums2'

# Use the 'filter()' function with a lambda function to find the intersection of 'array_nums1' and 'array_nums2'
# Filter out elements from 'array_nums2' that are also present in 'array_nums1' using the lambda function
result = list(filter(lambda x: x in array_nums1, array_nums2))

# Display the intersection of the two arrays ('array_nums1' and 'array_nums2')
print("\nIntersection of the said arrays: ", result) 

Sample Output:

Original arrays:
[1, 2, 3, 5, 7, 8, 9, 10]
[1, 2, 4, 8, 9]

Intersection of the said arrays:  [1, 2, 8, 9]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create Fibonacci series upto n using Lambda.
Next: Write a Python program to rearrange positive and negative numbers in a given array using Lambda.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/lambda/python-lambda-exercise-11.php