In Python, multiple functions are available that assist in completing simple to difficult tasks. To apply a specific function on all the elements of an iterable such as set, list, tuple, etc., the inbuilt “map()” function is used in Python. The map() function can apply certain operations on all items of the iterable object.
This write-up will give you an in-depth overview of the Python map() function with numerous examples. The following aspects are discussed in this Python article:
- What is a Python map() Function?
- Example 1: Simple Python map() Function
- Example 2: Mapping an Iterable Using Single Line Expression (Lambda Function)
- Example 3: Python map() Function to Multiply Two Iterable Objects
Let’s get started!
What is a Python map() Function?
The “map()” function executes a specified operation for every element of an iterable object. The items of the iterable object are passed as a parameter to the function. The syntax of the Python map() function is shown below:
In the above syntax:
- The function which uses the element of iterable for its expression execution is placed at parameter “function”.
- The iterable such as set, list, tuple, sequence, or any object iterator to be mapped are placed at parameter “iterable”.
- We can pass more than one iterable object as a parameter value of the “map()” function.
Let’s have a look at the examples given below for a better understanding of the “map()” function.
Example 1: Simple Python map() Function
In the example given below, the “map()” function is used to execute a specified function on the given iterable, and the “list()” function is used to return the map result into the list.
def multiply(num): return num * num tuple_num = (2, 4, 5, 10) output = map(multiply, tuple_num) print(list(output))
In the above code:
- The function named “multiply” is created using the prefix “def” at the start.
- The algebraic expression “num” * “num” returns the multiplied value to the function using the “return” statement.
- The “tuple” is initialized with a numeric value.
- The “map()” function takes the function name as the first parameter and the initialized tuple variable as a second parameter. This “map()” function executes the function for each item of the initialized tuple.
- After applying mapping on the tuple, the final result will be converted into a list using the list() function.
After applying a specific function to a tuple iterable, the output above shows the list.
Example 2: Mapping an Iterable Using Single Line Expression (Lambda Function)
An anonymous function named “lambda” is defined without using any name in Python as a single-line expression. This function reduces the multiple lines of expression to a single line in Python. In the example below, the Python “map()” function is used along with the lambda expression.
#create tuple tuple_num = (12, 14, 15, 10) #using map() with lambda output = map(lambda x: x * x, tuple_num) #using list() function print(list(output))
In the above code:
- The tuple is initialized and stored in a variable named “tuple_num”.
- The “map()” function is used along with the lambda expressions. An anonymous function is defined with the keyword “lambda” and the expression of the function is initialized after using the colon ”:”.
- The “map()” function takes the lambda function and tuple as an argument and executes the expression of the lambda function on each item of the tuple iterable.
In the above output, the tuple items are squared using map and lambda.
Example 3: Python map() Function to Multiply Two Iterable Objects
In the example given below, the “map()” function is used to multiply the elements of two lists and returns the value into a separate list using the “list()” function.
Note: Python map() function is also used to multiply other iterable like tuples, sets, dictionaries, etc., using the same method below.
first_list = [5, 12, 23] second_list = [4, 15, 62] output = map(lambda a, b: a * b, first_list, second_list) print(list(output))
In the above code:
- Two lists containing numeric values are initialized in the code.
- Python map() function takes the “lambda” function, “first_list”, and “second_list” variable as an argument.
- The lambda function has two arguments, “x” and “y”, for each element of the list, and the expression of the function is defined as the multiplication of “a” and “b”.
- The “map()” function executes the specified function on each list element and adds the element of both lists, and returns it to the list using the “list()” function.
In the above output, the two elements of the list are added and returned to a separate list.
That is all from this guide!
The “map()” function is used to execute a specified function on each element of an iterable such as a Tuple, List, Dictionary, Tuple, etc. The “map()” function takes the value of the defined function and an iterable object as an argument. It returns the output results as a new iterator after applying the function on each element of the iterable. The “map()” function is also used along with the anonymous “lambda” function by taking it as an argument along with an iterable variable. This write-up presented an in-depth understanding of Python’s “map()” function with multiple examples.