Blog Post #130: Functional Programming 101: The map() Function

In our discussions on comprehensions and generators, we’ve touched on ideas from a programming paradigm called “functional programming.” This style emphasizes transforming data by passing it through a series of functions. Python isn’t a purely functional language, but it includes several powerful built-in functions that support this clean and expressive style of coding.

In this post, we’ll learn about one of the most fundamental functional tools: the map() function. It provides a concise way to apply a function to every item in a list or other iterable.

What Does map() Do?

The map() function takes two main arguments: a function and an iterable (like a list).

The syntax is: map(function, iterable)

Its job is to call the provided function on every single item from the iterable and gather the results.

Importantly, map() is “lazy,” just like the generators we learned about in Post #126. It doesn’t compute all the results at once and store them in a list. Instead, it returns a special map object, which is an iterator. This iterator produces the results one by one as you ask for them, making it very memory-efficient. To see all the results at once, we typically convert this iterator into a list using list().

map() in Action

Let’s look at a few common use cases.

Using map() with a Built-in Function

A very common task is to convert a list of items from one data type to another. For example, you might have a list of number strings that you received from user input, and you need to convert them all to integers.

number_strings = ["10", "25", "50", "80"]

# We can "map" the built-in int() function onto every item in our list
int_iterator = map(int, number_strings)

# To see the result, we convert the iterator to a list
int_numbers = list(int_iterator)

print(int_numbers)

The output is a new list of integers: [10, 25, 50, 80]. This is much more concise than writing a for loop to do the conversion.

Using map() with Your Own Function

You can also use map() with any function you define yourself. Let’s create a function to square a number and then map it across a list of numbers.

def square(number):
    return number ** 2

numbers = [1, 2, 3, 4, 5]

# Apply our custom 'square' function to each number
squared_numbers = list(map(square, numbers))

print(squared_numbers)

The output will be: [1, 4, 9, 16, 25].

Using map() with a lambda Function

For simple, one-off functions like square, defining a full def statement can feel a bit verbose. This is a perfect use case for a lambda function, which we learned about in Post #106.

We can rewrite the previous example more concisely by defining the squaring logic directly inside the map() call.

numbers = [1, 2, 3, 4, 5]

# The lambda function x: x ** 2 is the transformation we want to apply
squared_numbers = list(map(lambda x: x ** 2, numbers))

print(squared_numbers)

This is a very common and powerful pattern in functional-style Python code.

What’s Next?

The map() function provides a functional way to apply a transformation to every element in a sequence. It’s a lazy, memory-efficient tool that pairs beautifully with simple def functions or concise lambda expressions.

map() is for transforming every item in a sequence. But what if we want to select a subset of items from that sequence based on a condition? For this, Python provides another core functional tool. In Post #131, we will explore the filter() function.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment