DEV Community

Nick Langat
Nick Langat

Posted on

Exploring Python's Map Built in

This article first appeared on my personal blog here.

Introduction.

Python's standard library comes with a very useful built-in method called map() that we will take a tour of in this article.

By way of functionality, let's think of the map function this way:
It takes a function as its first argument and an iterable as second and then it applies the function to each member of the iterable in question.

That's the top level overview of the map built-in.

Now let's look at a few data problems, how we would transform them without map and then how we would transform them using map. Sounds good? Let's dive in.

Sample Problem:

Supposing you have a string of words like below:

title = "uses of strings in python"
Enter fullscreen mode Exit fullscreen mode

Then you are asked to transform each word and capitalize only the first letter such that you end up with:

title = "Uses Of Strings In Python"
Enter fullscreen mode Exit fullscreen mode

Without using map(), we can do something like:

title = "uses of strings in python"

title_list = title.split()
new_title = ""
for word in title_list:
    new_title  += f" {word.capitalize()}"

print(new_title)
#Uses Of Strings In Python
Enter fullscreen mode Exit fullscreen mode

Which works, no problem. However, map()is well suited for such classic problems and we can leverage its power here. If you recall, map() takes a function and an iterable and returns a generator object having applied the function to all members of the iterable. With this in mind, we will follow the approach listed below:

We start by converting the string of words into a list by calling list.split().

We then apply str.capitalize() to each word in our list above. This will be our function.

We then call map() supplying our custom function and list above.
We then turn the generator object into a list.
We then call list.join() to get back our string of words.

The code for the above process will look like:

title_list = title.split()

new_title = " ".join(list(map(str.capitalize, title_list)))

print(new_title)
# Uses Of Strings In Python

Alternatively, you can use lambda() function to achieve the same effect, thus:

title_list = title.split()

new_title = " ".join(list(map(lambda x: x.capitalize(), title_list)))

print(new_title)
# Uses Of Strings In Python
Enter fullscreen mode Exit fullscreen mode

It is also possible to have a user defined function then pass it to map() and it will give the same result. Consider:

def capitalize_word(word):
    return word.capitalize()

title_list = title.split()

new_title = " ".join(list(map(capitalize_word, title_list)))

print(new_title)
# Uses Of Strings In Python
Enter fullscreen mode Exit fullscreen mode

Recap

In this article, we learnt what the map() does in Python from a high level point of view. We then saw an example problem where we put map() to use. Some key take aways here include:

  • map() takes a function and an iterable or iterables and then applies the function to each member of the iterable.
  • map() returns a map object which is a generator object. This can then be made into an iterable by applying the list().
  • map() works well with lambda(), other Python built-ins but can also accept a user defined function as its first argument.

Thank you for following this article to the end. I'll see you on the next one!

Top comments (0)