map()
The built-in map()
function in Python allows you to apply a transformation function to each item of one or more iterables, producing an iterator that yields transformed items.
This function is particularly useful for processing and transforming data in a functional programming style without explicitly using a for
loop. Here’s an example of using map()
to square numbers in a list:
>>> numbers = [1, 2, 3, 4, 5]
>>> list(map(lambda x: x**2, numbers))
[1, 4, 9, 16, 25]
map()
Signature
map(function, iterable, *iterables)
Arguments
Argument | Description |
---|---|
function |
A callable that takes as many arguments as there are iterables. |
iterable |
An iterable such as a list, tuple, etc. |
*iterables |
Additional iterables of the same length as iterable . |
Return Value
- A
map
object that generates items by applying the transformation function to every item of the input iterable or iterables.
map()
Examples
With built-in functions and a single iterable as arguments:
>>> list(map(float, ["12.3", "3.3", "-15.2"]))
[12.3, 3.3, -15.2]
>>> list(map(int, ["12", "3", "-15"]))
[12, 3, -15]
With a custom function and a single iterable as arguments:
>>> def square(number):
... return number ** 2
...
>>> numbers = [1, 2, 3, 4, 5]
>>> list(map(square, numbers))
[1, 4, 9, 16, 25]
With a lambda
function and a single iterable as arguments:
>>> list(map(lambda x: 2**x, [1, 2, 3, 4, 5, 6, 8]))
[2, 4, 8, 16, 32, 64, 256]
With multiple iterables as arguments:
>>> first_it = [1, 2, 3]
>>> second_it = [4, 5, 6, 7]
>>> list(map(pow, first_it, second_it))
[1, 32, 729]
map()
Common Use Cases
The most common use cases for the map()
function include:
- Applying a function to transform all items in an iterable.
- Processing multiple iterables in parallel.
- Performing calculations on data elements without using explicit loops.
map()
Real-World Example
Suppose you have a list of temperatures in Celsius, and you need to convert them into Fahrenheit. You can use map()
to apply the conversion function across the entire list:
>>> def to_fahrenheit(celsius):
... return round((9/5) * celsius + 32, 1)
...
>>> celsius = [0, 20, 37, 100]
>>> list(map(to_fahrenheit, celsius))
[32.0, 68.0, 98.6, 212.0]
In this example, you use map()
to convert a list of temperatures in Celsius into Fahrenheit by applying the to_fahrenheit()
function to each item in the input list.
Related Resources
Tutorial
Python's map(): Processing Iterables Without a Loop
In this step-by-step tutorial, you'll learn how Python's map() works and how to use it effectively in your programs. You'll also learn how to use list comprehension and generator expressions to replace map() in a Pythonic and efficient way.
For additional information on related topics, take a look at the following resources:
- Functional Programming in Python: When and How to Use It (Tutorial)
- Functional Programming in Python (Course)
- Iterators and Iterables in Python: Run Efficient Iterations (Tutorial)
- How to Use Python Lambda Functions (Tutorial)
- Python's map() Function: Transforming Iterables (Course)
- Functional Programming in Python: When and How to Use It (Quiz)
- Efficient Iterations With Python Iterators and Iterables (Course)
- Iterators and Iterables in Python: Run Efficient Iterations (Quiz)
- Using Python Lambda Functions (Course)
- Python Lambda Functions (Quiz)