Python                                                          Home : www.sharetechnote.com

Python - Map

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

• Applying a map with normal function with one argument  - Example 1
• Applying a map with lambda function with one argument - Example 2
• Applying a map with normal function with multiple argument  - Example 3
• Applying a map with lambda function with multiple argument - Example 4
• Printing out all the items in a Mapped Object - Example 5
• Printing out all the items in a Mapped Object - Example 6
• Printing out all the items in a Mapped Object - Example 7
• Indexing items in a Mapped Object - Example 8

Examples :

 < Example 1 >   def f(x):     y = x^2 + 2*x + 3     return y   xList = [-3,-2,-1,0,1,2,3]   yList = map(f,xList) print(yList) print(list(yList))   Result :---------------------------------- [2, -1, -4, 5, 6, 11, 8]     < Example 2 >   xList = [-3,-2,-1,0,1,2,3]   yList = map(lambda x: x^2 + 2*x + 3,xList) print(yList) print(list(yList))   Result :---------------------------------- [2, -1, -4, 5, 6, 11, 8]       def timeStamp(y,m,d):     tStamp = str(y) + "-" + m + "-" + str(d)     return tStamp   yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(timeStamp,yList,mList,dList) print(tStamp) print(list(tStamp))   Result :---------------------------------- ['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7']       yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList) print(tStamp) print(list(tStamp))   Result :---------------------------------- ['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7']       yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)   for index, item in enumerate(tStamp):    print(index,',',item)        Result :---------------------------------- 0 , 2017-Jan-1 1 , 2017-Feb-3 2 , 2017-Mar-6 3 , 2017-Apr-7     < Example 6 >   yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)   for item in enumerate(tStamp):    print(item)     Result :---------------------------------- (0, '2017-Jan-1') (1, '2017-Feb-3') (2, '2017-Mar-6') (3, '2017-Apr-7')     < Example 7 >   yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)   for item in enumerate(tStamp):     print(item[0],',',item[1])     Result :---------------------------------- 0 , 2017-Jan-1 1 , 2017-Feb-3 2 , 2017-Mar-6 3 , 2017-Apr-7       yList = [2017,2017,2017,2017] mList = ["Jan","Feb","Mar","Apr"] dList = [1,3,6,7];   tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)   tStampList = list(tStamp);   print(tStampList) print(tStampList[0]) print(tStampList[1:3])     Result :---------------------------------- ['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7'] 2017-Jan-1 ['2017-Feb-3', '2017-Mar-6']     < Example xx >