Introduction
The Python map()
function allows you to apply a transformation to each item of an iterable. The result is a new iterable object with all the transformed items. For example, if you have a list of words, you can use map()
to capitalize each one of those words. map()
is a built-in Python function that does not require an import from a module.
The map()
function is a programming construct that eliminates the need for explicit loops and provides a concise way to transform data. It has been implemented in many programming languages because this operation (i.e. applying the same transformation to each item of a collection) is a very common need and simplifies code that would otherwise use a loop.
Basic Python map() Function Syntax
The basic syntax of the map()
function involves passing a function and an iterable as arguments. The function is applied to each element of the iterable, and the results are returned as a new iterable. The function can be a function you define with def
, a lambda expression, or any other function.
map(function, iterable)
function
: The function to apply to each element of the iterable.iterable
: The original iterable whose elements will be processed.
The following example squares each number in a list.
Example:
numbers = [1, 2, 3, 4, 5] squares = map(lambda x: x**2, numbers) print(list(squares))
Output:
[1, 4, 9, 16, 25]
In this example:
lambda x: x**2
: The lambda function defines the squaring operation.numbers
: The original list of numbers.map(lambda x: x**2, numbers)
: Applies the squaring function to each element in the list.list(squares)
: Converts the map object to a list, revealing the squared values.
Note that the result of map had to be converted to a list with the list()
constructor in order to be printed out. This is because the result of map()
is an iterable, which has no meaningful default printout. Its printout is just object information. Let’s see what happens when we directly print the iterable returned by map()
:
Example:
print(squares)
Output:
<map object at 0x7f848d2c9f10>
This reveals that squares
is a map
object, which is just an iterable created by the map()
function.
The next example demonstrates how you can use map()
with a custom function you define.
Example:
def celsius_to_fahrenheit(celsius): return (9/5) * celsius + 32 temperatures_celsius = [0, 10, 20, 30, 40] temperatures_fahrenheit = map(celsius_to_fahrenheit, temperatures_celsius) print("Temperatures in Celsius:", temperatures_celsius) print("Temperatures in Fahrenheit:", list(temperatures_fahrenheit))
Output:
Temperatures in Celsius: [0, 10, 20, 30, 40] Temperatures in Fahrenheit: [32.0, 50.0, 68.0, 86.0, 104.0]
In this example:
- We define a custom function
celsius_to_fahrenheit()
that takes a temperature in Celsius as an argument and returns the equivalent temperature in Fahrenheit. - The
temperatures_celsius
list contains temperatures in Celsius. - The
map()
function applies thecelsius_to_fahrenheit()
function to each element intemperatures_celsius
. - The result is a
map
object, which we convert to a list for display.
Differences Between map() and List Comprehensions
While map()
and list comprehensions can achieve similar results, there are key differences:
- Function vs. Expression:
map()
requires a function, which can be a built-in function, a custom-defined function or a lambda.- List comprehensions use expressions, allowing for more concise syntax.
- Output Type:
map()
returns a map object, which can be converted to other iterables like lists or tuples.- List comprehensions directly generate lists.
Using map() with Multiple Iterables
The map()
function can also handle multiple iterables, applying the provided function to elements from each iterable in parallel. (If one iterable has more elements than the other, the extra ones are ignored.)
Example:
numbers1 = [1, 2, 3] numbers2 = [4, 5, 6] sums = map(lambda x, y: x + y, numbers1, numbers2) print(list(sums))
Output:
[5, 7, 9]
In the example above, the lambda function takes two arguments, and map()
combines elements from numbers1
and numbers2
element-wise, producing a list of sums.
Summary & Reference for the Python map() Function
The map()
function applies a transformation to each item of an iterable. It returns a new iterable object whose items reflect that transformation.
map()
is a built-in function and does not require importing any modules.
The basic map()
function syntax involves passing a function (which can be a lambda expression or any other function type) and an iterable: map(function, iterable)
numbers = [1, 2, 3, 4, 5] squares = map(lambda x: x**2, numbers) # squares --> [1, 4, 9, 16, 25]
Differences between map()
and list comprehensions include the requirement for a function to be given, and the output being an iterable instead of a list.
map()
can handle multiple iterables in parallel, applying the provided function to corresponding elements.
numbers1 = [1, 2, 3] numbers2 = [4, 5, 6] sums = map(lambda x, y: x + y, numbers1, numbers2) # sums --> [5, 7, 9]