Python’s map()
is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for
loop, a technique commonly known as mapping. map()
is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable. map()
is one of the tools that support a functional programming style in Python.
In this tutorial, you’ll learn:
- How Python’s
map()
works - How to transform different types of Python iterables using
map()
- How to combine
map()
with other functional tools to perform more complex transformations - What tools you can use to replace
map()
and make your code more Pythonic
With this knowledge, you’ll be able to use map()
effectively in your programs or, alternatively, to use list comprehensions or generator expressions to make your code more Pythonic and readable.
For a better understanding of map()
, some previous knowledge of how to work with iterables, for
loops, functions, and lambda
functions would be helpful.
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you'll need to take your Python skills to the next level.
Coding With Functional Style in Python#
In functional programming, computations are done by combining functions that take arguments and return a concrete value (or values) as a result. These functions don’t modify their input arguments and don’t change the program’s state. They just provide the result of a given computation. These kinds of functions are commonly known as pure functions.
In theory, programs that are built using a functional style will be easier to:
- Develop because you can code and use every function in isolation
- Debug and test because you can test and debug individual functions without looking at the rest of the program
- Understand because you don’t need to deal with state changes throughout the program
Functional programming typically uses lists, arrays, and other iterables to represent the data along with a set of functions that operate on that data and transform it. When it comes to processing data with a functional style, there are at least three commonly used techniques:
-
Mapping consists of applying a transformation function to an iterable to produce a new iterable. Items in the new iterable are produced by calling the transformation function on each item in the original iterable.
-
Filtering consists of applying a predicate or Boolean-valued function to an iterable to generate a new iterable. Items in the new iterable are produced by filtering out any items in the original iterable that make the predicate function return false.
-
Reducing consists of applying a reduction function to an iterable to produce a single cumulative value.
According to Guido van Rossum, Python is more strongly influenced by imperative programming languages than functional languages:
I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn’t view Python as a functional programming language. (Source)
However, back in 1993, the Python community was demanding some functional programming features. They were asking for:
- Anonymous functions
- A
map()
function - A
filter()
function - A
reduce()
function
These functional features were added to the language thanks to the contribution of a community member. Nowadays, map()
, filter()
, and reduce()
are fundamental components of the functional programming style in Python.
In this tutorial, you’ll cover one of these functional features, the built-in function map()
. You’ll also learn how to use list comprehensions and generator expressions to get the same functionality of map()
in a Pythonic and readable way.
Getting Started With Python’s map()
#
Sometimes you might face situations in which you need to perform the same operation on all the items of an input iterable to build a new iterable. The quickest and most common approach to this problem is to use a Python for
loop. However, you can also tackle this problem without an explicit loop by using map()
.
In the following three sections, you’ll learn how map()
works and how you can use it to process and transform iterables without a loop.
Understanding map()
#
map()
loops over the items of an input iterable (or iterables) and returns an iterator that results from applying a transformation function to every item in the original input iterable.
According to the documentation, map()
takes a function object and an iterable (or multiple iterables) as arguments and returns an iterator that yields transformed items on demand. The function’s signature is defined as follows:
map(function, iterable[, iterable1, iterable2,..., iterableN])
Read the full article at https://realpython.com/python-map-function/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Real Python
read more
No comments:
Post a Comment