## Python Map() Function

Last Updated On Friday 14th Jan 2022

## Python map

Map Function in Python

In this tutorial, we will learn about the Python map() function with the help of examples.

The map() function executes a specified function for each item in an iterable and returns an iterators.

Syntax

	map(function, iterables)



### Map Python

Parameter

• function (Required) : The function to execute for each item
• iterable (Required): An iterable like sets, lists, tuples, etc

### python list map

Returns

• The map() function is going to apply the given function on all the items inside the iterator and return an iterable map object,example tuple, a list, etc.
• The map function, allows us to apply the same operation to each element and return a new list of elements receiving the modified elements from this operation.

### Map in Python

Imagine that we have a list of names.

	names = ['RadCliffe', 'Craig', 'Dae Kim', 'Gilles', 'Sharman']



Now we can iterate through the names one by one, add the first name “Daniel”.

	def addDaniel(name):
return "Daniel " + name


	print(map(addDaniel, names))
# <map object at 0x7f1e182ec1f0>



### Python Map Function

The map function returns a map object. So, in order to get our desired list back, we need to coerce the map object to a list.

	print(list(map(addDaniel, names)))


	[
'Daniel Craig',
'Daniel Dae Kim',
'Daniel Gilles',
'Daniel Sharman'
]



### Map Function Python

• So the map function goes through each element and executes the altering function on the current element.
• Then the return value of the altering function is added as an element to the new list.

### python map()

	def add(n):
return n + n

numbers = (9, 5, 7, 3)
print(list(result))

# [18, 10, 14, 6]



### python map list

	def myfunc(n):
return len(n)

names = ['RadCliffe', 'Craig', 'Dae Kim', 'Gilles', 'Sharman']

print(list(map(myfunc, names)))

# [9, 5, 7, 6, 7]