# Understand the map function in Python

Tyler (275)
0

This guide provides an overview of Python map function. Python 2 and Python 3 have subtle differences in implementation, but they are generally used in the same way.

Here's everything you'll need to complete this guide:

 python × 1

## Basic implementation

The purpose of the map function is to apply the same procedure to every item in an iterable data structure. Iterable data structures can include lists, generators, strings, etc. In a very basic example, map can iterate over every item in a list and apply a function to each item. Then it will return the new list. Assume you have a function that takes an integer and increments it by 1:

def incr_by_one(num): return num + 1

You also have a list of integers representing something very important:

important_list = [1, 2, 4, 6, 10, 12, 16, 18]

If you need to increment every item in this list by 1 you can do this with map:

map(incr_by_one, important_list)

You can see that map takes a minimum of two arguments. The first is the function name, and the second is the list. Map will iterate through every item of important_list and pass the element to incr_by_one. Then it will append the output of the incr_by_one function to a new list. Once it finishes iterating it will return a new list leaving the original unaffected. So this map function will return:

[2, 3, 5, 7, 11, 13, 17, 19]

## Multiple iterables

The map function accepts multiple iterables. These can be passed in as optional arguments. If multiple iterables are passed in, an element from each is passed in as an argument to the designated function. Assume we have the following lists:

l1 = [1, 2, 3, 4, 5] l2 = [11, 12, 13, 14, 15] l3 = [21, 22, 23, 24, 25]

With map we can create a new list that contains the addition of elements at each index. So in our resulting list at index 0, we will have the sum of l1[0], l2[0], and l3[0]. This will continue for each element in the list. In order for this to work, the function we pass in must take the same number of arguments as the number of iterables you pass in. So if you pass in three lists, your function must take three arguments. Let's create our function:

def add_three_numbers(num1, num2, num3): return num1 + num2 + num3

Now that we have our function we can use map to generate our desired list:

This will produce:

[33, 36, 39, 42, 45]

You may have noticed that each of our lists has exactly five elements. So the map function worked very nicely. Its important to note how map handles iterables of different sizes. This is where Python 2 and Python 3 are different. Python 2 will iterate through the longest iterable and pass None in as the argument of the short iterables. So if one list has a length of seven and another list has a length of five, map will iterate through seven times but pass in None for the indices of the shorter list that exceed its length. If this is a possibility, your function must be prepared to handle arguments of NoneType (ours would throw a TypeError). Python 3 is a much cleaner implementation. It will only iterate through the length of the shortest iterable. So if you have a list of seven and a list of five, it will only iterate through five times. Read more about Python 2 map Read more about Python 3 map

Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Continuous integration is pretty trendy, but despite that fact, it's also pretty useful. This guide will show you how to set up your first project in CircleCI.
Tyler
In these interests: devopscirclecicode
jQuery is a powerful tool for front-end developers, but it does not alleviate the responsibility of ensuring your code is efficient.
Tyler
In these interests: javascriptjquerycode
Bourbon is a library of Sass mixins. It provides a lot of functionality while keeping your css slim. Assuming you're using a standard rails installation, Sass will already be installed and working.
Tyler
In these interests: codewebdevrails
Closures are extremely valuable if you know how to use them. The problem is that many JavaScript developers don't know how to use them.
Ruby on Rails is one of the most popular web development frameworks, and Heroku has become a popular place to quickly deploy applications - and both for very good reason.
You could write JavaScript for years without ever understanding scope or how the this keyword is set (it probably won't be great code).
If you're a Python programmer, you should become familiar with iPython. Use the steps in this guide to learn how to install iPython. What is iPython?
In short, hoisting is when JavaScript moves variable and function declarations to the top of their scope before any code is executed.
A scope is simply the set of variables that you have access to. Scoping in JavaScript can be confusing. Even some experienced JavaScript developers still struggle with it.
Learn how to use Python's sleep function to make a timed delay. tl;dr import time time.sleep(seconds)
Posted in these interests:
code
PRIMARY
Code is poetry -- one line at a time.
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
Discuss this guide:
We're hiring!
Are you a passionate writer or editor? We want to hear from you!

Follow @howchoo and learn cool things:

Like what we do?

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.