- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
The intersection of two arrays in Python (Lambda expression and filter function )
In this article, we will learn about the intersection of two arrays in Python with the help of Lambda expression and filter function.
The problem is that we are given two arrays we have to find out common elements in both of them.
1. Declaring an intersection function with two arguments. 2. Now we use the lambda expression to create an inline function for selection of elements with the help of filter function checking that element is contained in both the list or not. 3. Finally, we convert all the common elements in the form of a list by the help of typecasting. 4. And then we display the output by the help of the print statement.
Now let’s take a look at its implementation:
def interSection(arr1,arr2): # finding common elements # using filter method oto find identical values via lambda function values = list(filter(lambda x: x in arr1, arr2)) print ("Intersection of arr1 & arr2 is: ",values) # Driver program if __name__ == "__main__": arr1 = ['t','u','t','o','r','i','a','l'] arr2 = ['p','o','i','n','t'] interSection(arr1,arr2)
Intersection of arr1 & arr2 is: ['o', 'i', 't']
In this article, we learned about the intersection of two arrays in Python with the help of Lambda expression and filter function and its implementation.
- Related Articles
- Intersection of Two Arrays II in Python
- Lambda and filter Examples in Python
- Lambda and filter in Python Examples
- Map function and Lambda expression in Python to replace characters
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of Two Arrays in C++
- Find Union and Intersection of two unsorted arrays in C++
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- How to find the intersection of two arrays in java?
- How to get the intersection of two arrays in MongoDB?
- Intersection() function Python
- C++ program to find union and intersection of two unsorted arrays
- How to use Function and BiFunction interfaces in lambda expression in Java?