Python Map Function
Tags: pythontutorials, python, pythonmap, pythonmapexample
Published: 2019-11-01 16:11:55

---
title: Python Map Function
published: true
date: 2019-10-07 10:35:27 UTC
tags: Python Tutorials,python,python map,python map example
canonical_url: https://pythongeeks.net/python-tutorials/python-map-function/
---

<g id="20" data-gr-id="20">Python </g>`map`<g id="20" data-gr-id="20"> function</g> is used to execute a function on all elements of an iterator. The function to execute will be passed <g id="9" data-gr-id="9">to </g>`map()`<g id="9" data-gr-id="9"> as</g> a parameter. Through this blog post, we will <g id="10" data-gr-id="10">understand </g>`map()`<g id="10" data-gr-id="10"> function</g> via many examples in coding.

**A Quick Example:**

```python
def newfunc(val):
    return val * 2 
result = map(newfunc, [1, 2, 3, 4]) 
print(list(result))

# Result: [2, 4, 6, 8]
```

![Python map function likes a short hand of a loop](https://i1.wp.com/pythongeeks.net/wp-content/uploads/2019/10/tower_bridge_sunset-wallpaper-1920x1080.jpg?resize=640%2C360&ssl=1)<figcaption>Lambda function likes a short hand of a loop.<br>(London – Source: <a href="http://wallpaperswide.com/">Wallpapers Wide</a>) </figcaption>

### Definition and Usage

In python, map function syntax is quite simple:

```python
map(function, iterators)
```

- function: Required and will execute for items of iterators
- iterators: Required. You can send many iterators. H<g id="63" data-gr-id="63">owever</g>, the function must have one parameter for each iterator.

### Python Map Examples

#### **Example 1: How to use lambda function with map()**

In the above example, we can make it shorter, cleaner with a lambda function.

```python
result = map(lambda x: x * 2, [1, 2, 3, 4]) 
print(list(result)) 

# Result: [2, 4, 6, 8]
```

If you are new to Python, you should read this article for more detail of lambda function ([What is lambda function?](https://pythongeeks.net/python-tutorials/python-lambda-function/))

#### **Example 2: Passing multiple Iterators to map() function**

Lambda function should have one parameter for each iterator.

```python
iter1 = [1, 5, 7]
iter2 = [9, 5, 3]

result = map(lambda x, y: x + y, iter1, iter2)
print(list(result)) 

# Result: [10, 10, 10]
```

#### **Example 3: Passing multiple Iterators with different size**

Let's see what happen if we pass multiple iterators with different size.

In python 2, we got an exception. Therefore, be aware of this case in your code.

```python
iter1 = [1, 5, 7, 9, 8]
iter2 = [9, 5, 3] 

result = map(lambda x, y: x + y, iter1, iter2) 
print(list(result)) 

"""
Result: 
   Traceback (most recent call last): File "main.py", line 6, in \<module\> result = map(lambda x, y: x + y, iter2, iter1) File "main.py", line 6, in \<lambda\> result = map(lambda x, y: x + y, iter2, iter1)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
"""
```

In python 3, we get a map object with the size of smallest iterator

```python
iter1 = [1, 5, 7, 9, 8]
iter2 = [9, 5, 3]

result = map(lambda x, y: x + y, iter1, iter2)
print(list(result)) 

# Result: [10, 10, 10]
```

### References

In this post, I provided some simple examples of the python map function. However, you can get more details from these links:

- [Python Official Document](https://docs.python.org/3/library/functions.html#map)
- [Python 2 Official Document](https://docs.python.org/2/library/functions.html#map)

The post [Python Map Function](https://pythongeeks.net/python-tutorials/python-map-function/) appeared first on [Python Geeks](https://pythongeeks.net).