Ekta Aggarwal

# map function in Python

Map( ) in Python is used to apply an in-built or user defined or lambda functions to an iterator object (eg. lists, sets, tuples etc.). It is highly useful to write shorter and elegant Python codes avoiding for loops.

__Syntax:__

*map( function, iterator object1, iterator object 2, ....)*

__Key Topics covered:__

__Key Topics covered:__

**Understanding with examples!**

**Task: **Return the square of the numbers in a list.

Let us create a UDF **square_func **which returns the square of a number

```
def square_func(n):
return(n**2)
```

Let us create our iterator object (our list) named **numbers:**

` numbers = [3,4,5,6]`

Let us apply map( ) function.

```
result = list(map(square_func, numbers) )
â€‹result
```

Note that by default map( ) function returns a generator object, thus we need to pass on this generator output to list( ) or set( ) .

__Output:__

`[9, 16, 25, 36]`

__Lambda function:__

__Lambda function:__

Alternatively, we can pass a **lambda function ** inside our map( ) function:

```
numbers = [3,4,5,6]
result = map(lambda x: x**2, numbers)
print(list(result))
```

__Output:__

`[9, 16, 25, 36]`

**Passing in-built Python functions to map( ) **

**Task: **Return the absolute numbers in a list.

Let us create our list

`mylist = [-20,-10,-5,5,2,3]`

We can use Python's inbuilt **abs** function to get absolute numbers.

`list(map(abs,mylist))`

__Output:__

`[20, 10, 5, 5, 2, 3]`

**Task: **Return the list where all the elements are in upper case.

`mylist = ['Hello','how','are','You','doing?']`

We can Python's inbuilt function str.upper to our map( ) function:

`list(map(str.upper,mylist))`

__Output:__

`['HELLO', 'HOW', 'ARE', 'YOU', 'DOING?']`

**Task: **Return the list where all the elements denote the length of the elements in mylist.

`list(map(len,mylist))`

__Output:__

`[5, 3, 3, 3, 6]`

**Multiple iterator objects in map( ) **

In Python we can pass multiple iterator objects (lists, sets, tuples) to map function. In such a case we need to take care that our in-built or lambda or UDF inside map should be able to take as many arguments we pass as iterators.

In this case each value from the iterator will be passed together to the function and the output would be generated.

Let us create a list of 5 elements:

`mylist = [2.64543,4.253,6.336786,3.7556774,2.6356799672]`

range(1,6) returns a range from 1 to 5 (6 is excluded)

`list(map(round, mylist, range(1,6)))`

â€‹__Output:__

`[2.6, 4.25, 6.337, 3.7557, 2.63568]`

** Explanation of the output: **Here first element of mylist and second iterator i.e. range(1,6) i.e. (2.64543 and 1) get passed together to round function and output is 2.6.

Then second element from both the iterators (4.253 and 2) get passed together and output is 4.25.

Similarly the output is generated one by one for each element.

__What if we provide iterators of unequal length?__

__What if we provide iterators of unequal length?__

In such a scenario Python will truncate the output as soon as it reaches the end of the iterator having smallest number of elements.

Our list has 5 elements:

`mylist = [2.64543,4.253,6.336786,3.7556774,2.6356799672]â€‹`

While range(1,4) is an iterator of 3 elements:

`list(map(round, mylist, range(1,4)))`

Thus **map( ) **will return 3 elements only.

__Output:__

`[2.6, 4.25, 6.337]`

Similarly, we first iterator has length 5

`mylist = [2.64543,4.253,6.336786,3.7556774,2.6356799672]`

While second iterator has 9 elements:

`list(map(round, mylist, range(1,10)))`

Then map( ) will return only 5 elements:

â€‹__Output:__

`[2.6, 4.25, 6.337, 3.7557, 2.63568]`

__Lambda function with multiple iterators:__

__Lambda function with multiple iterators:__

Let us create 2 lists:

```
list1 = [1,2,3,4]
list2 = [10,20,30,40]
```

If we are passing 2 iterators then lambda function must be able to take 2 variables as input (in our case x and y) and following lambda function returns product of the corresponding elements:

`list(map(lambda x,y: x*y,list1, list2))`

__Output:__

`[10, 40, 90, 160]`

**What if our UDF returns multiple values?**

To understand this let us create a UDF which returns 2 values: double of a number and triple of a number:

```
def my_func(x):
return(x*2,x*3)
```

Let us create our list on which we want to carry out the operation:

`mylist = [1,2,3,4]`

`list(map(my_func,mylist))`

On applying map function the result is a list of tuples:

`[(2, 3), (4, 6), (6, 9), (8, 12)]`