Python Map Functions
Table Of Contents:
- What Is Map Function?
- Syntax Of Map Function.
- Examples Of Map Function.
(1) What Is A Map Function?
- The map( ) function is an in-built python function, which is used to apply a given function to each item of an iterable (list, tuple etc.)
- The map( ) function will return an iterator (list, tuple etc) as an output.
(2) Syntax Of Map Function?
map(function, iterable)
The map( ) Parameters:
- The map( ) function takes two parameters:
(1) function – a function that performs some action on each element of an iterable
(2) iterable – an iterable like sets, lists, tuples, etc. - You can pass more than one iterable to the map( ) function.
The map( ) Return Type:
- The map( ) function returns an object of the map class.
- That object can be passed to an iterable (a list, set, tuple or dictionary), to convert it into that iterable type.
(3) Examples Of Map Function?
Example-1:Cube Of Each Numbers
numbers = [1,2,3,4,5,6]
def cube(n):
return n * n * n
mapping = map(cube,numbers)
number_cubes = list(mapping)
number_cubes
Output:
[1, 8, 27, 64, 125, 216]
Note:
- Here, if you want to apply a cube of each number in a list, you can use the map function.
- First, you need to create a user-defined cube function, which will return the cube of a number.
- You need to pass the User Defined Function and the iterable to the map function.
- The map function will apply the cube function to each member of a list.
- The map( ) function will return a map object, which you can pass it to any iterable to convert that type.
Example-2:Square Root Of Each Number
import math
numbers = [1,2,3,4,5,6]
def square_root(n):
return math.sqrt(n)
mapping = map(square_root,numbers)
square_root_num = list(mapping)
print(square_root_num)
Output:
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178]
Note:
- In the above program, we have derived the square root of each number.
Example-3: Add A Number To Values Of Dictionary
marks = {'Subrat':57,
'Abhispa':65,
'Anuradha':87,
'Archana':99,
'Anamika':100}
def add_extra(n):
var = list(n)
var[1] = var[1] + 5
return tuple(var)
mapping = map(add_extra, marks.items())
marks = list(mapping)
print(marks)
Output:
[('Subrat', 62),
('Abhispa', 70),
('Anuradha', 92),
('Archana', 104),
('Anamika', 105)]
Note:
- In the above program, we have added extra 5 marks to each student.
Example-4: Convert Characters To Numbers
numbers = ['1','2','3','4','5']
def integer(n):
return int(n)
mapping = map(integer,numbers)
int_values = list(mapping)
print(int_values)
Output:
[1, 2, 3, 4, 5]
Note:
- In the above program, we have added extra 5 marks to each student.
Example-5: Convert Strings To Date Type
from datetime import datetime
dates = ['01-12-2022','02-12-2022','03-12-2011','04-12-2022']
def date_converter(date):
return datetime.strptime(date,'%d-%m-%Y')
mapping = map(date_converter,dates)
dates = list(mapping)
print(dates)
Output:
[datetime.datetime(2022, 12, 1, 0, 0), datetime.datetime(2022, 12, 2, 0, 0), datetime.datetime(2011, 12, 3, 0, 0), datetime.datetime(2022, 12, 4, 0, 0)]
Note:
- In the above program, we are converting strings into Date Types.