How to use Python Map – with example

  • Reading time:15 mins read

What is the Python Map function?

The Python map function is useful in executing a function on an iterable object. 

 

 

How to use the Python Map function?

If we have an iterable object, for instance, a list, the map function basically lets us go through each one of the elements and apply the function to it. The function allows us to virtually return any kind of object for us that we can interpret into a new list.

 

Learn how to use Python Map online

 

So the basic way that you do this is that we need to create a new variable and in this case, we will name it ‘newlist’ and then we pass in the map keyword. The next thing that we want to do is the function name and then the iterable object which is our ‘animals list.

 

 

However, when we run this we will get an error as shown above since we have not defined the function that we ought to map to the list of animals.

We can proceed and create a simple function named ‘caps’ that is going to capitalize each and every element on the list as shown below.

 

 

So as shown above when we print out the ‘newlist’ we’re going to get an object returned because the map function will create an object for us.

But because this is actually a list, we can call the list keyword here in order to return the actual list.

 

 

Example 2

We can see that this returns every item in this list into a capital letter.

 

Python Map function is a useful tool while coding

 

In this example let’s create a new list of numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].

So what we could do is let’s say we want to apply some kind of mathematical function to every number on this list that we have. We are going to write a simple function, named ‘timestwo’, that is going to take each of the elements from the list and return twice the number. 

We are now going to create another list called ‘results’ and equate it to the map function. Within the empty pair of parentheses, we can now pass in the name of the function that we have just created ‘timestwo’ followed by the name ‘numbers’ which represents the list of numbers that we created at the beginning.

 

 

As shown in the output the function is applied to every number in the list thus returning a new list in which every number is doubled.

The map function is especially useful when working with data frames using pandas.

 

Python Map function

 

Summary

This is how Python map works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.