Python map() function usage
Python map() Function Usage
1. What is the map() Function?
map()
is a built-in higher-order function in Python. It accepts two arguments: a function and an iterable (such as a list, tuple, or set).
map()
applies the first argument (a function) to each element of the second argument (an iterable) and returns an iterator containing the results of the function application.
The following is the syntax of the map()
function:
map(function, iterable)
2. Application Scenarios of the map()
Function
map()
function is very flexible and can be used in a variety of scenarios. Common applications include:
- Apply a function to each element of an iterable to produce a new iterable
- Perform data conversion on each element of an iterable, such as converting a number to a string or a string to an integer
- Apply a conditional test on each element of an iterable to produce a new iterable
Next, we will explain several usages of the map()
function in detail.
3. Applying a Function to Each Element of an Iterable
First, we define a function, square()
, that takes a number as an argument and returns the square of that number.
def square(x):
return x ** 2
Then, we use the map()
function to apply the square()
function to a list and return the result.
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
We can use the list()
function to convert the iterable into a list and output the result.
print(list(squared_numbers))
The output is:
[1, 4, 9, 16, 25]
In the above code, map(square, numbers)
applies the square()
function to each element of the numbers
list. The map()
function returns an iterator, <map object at 0x7fee1507c5b0>
, which we convert to a list using the list()
function.
4. Performing Data Transformations on Each Element of an Iterable Object
In addition to applying a function, the map()
function can also be used to perform data transformations on each element of an iterable object.
For example, let’s say we have a list of integers and want to convert it to strings.
numbers = [1, 2, 3, 4, 5]
str_numbers = map(str, numbers)
We can also use the list()
function to convert an iterator to a list and print the result.
print(list(str_numbers))
The output is:
['1', '2', '3', '4', '5']
In the above code, map(str, numbers)
converts each element in the integer list numbers
to a string. The map()
function returns an iterator, <map object at 0x7fee1507caa0>
, which we convert to a list using the list()
function.
5. Applying Conditional Tests to Each Element of an Iterable
In addition, the map()
function can be used to apply conditional tests to each element of an iterable, generating a new iterable.
For example, suppose we have a list of integers and want to retain only those elements greater than 3.
numbers = [1, 2, 3, 4, 5]
filtered_numbers = map(lambda x: x if x > 3 else None, numbers)
We can also use the list()
function to convert an iterable to a list and output the result.
print(list(filtered_numbers))
The output is:
[None, None, None, 4, 5]
In the above code, we use the anonymous function lambda
to perform a conditional check. map(lambda x: x if x > 3 else None, numbers)
performs a conditional check on each element in the integer list numbers
. If the element is greater than 3, it is retained; otherwise, it is set to None
. The map()
function returns an iterator <map object at 0x7fee1507cfd0>
, which we convert to a list using the list()
function.
6. Summary
Through this article, we’ve learned about the usage and application scenarios of the map()
function. The map()
function conveniently applies a function to each element of an iterable object, performs data transformations, or performs conditional checks. In actual programming, we can fully utilize the features of the map()
function to simplify code and improve readability.